Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 696 | Count Binary Substrings | Java | Easy | Two Pointers & Group Counting | CodeByTushu

Video information, playback and saved actions.

LeetCode 696 | Count Binary Substrings | Java | Easy | Two Pointers & Group Counting | CodeByTushu

Description: πŸš€ Welcome Back to CodeByTushu!

In today’s video, we solve LeetCode 696 β€” Count Binary Substrings using Two Pointers & Consecutive Group Counting in Java.

We are given a binary string s and our goal is to find the number of non-empty substrings that contain equal numbers of 0s and 1s, where all 0s and all 1s are grouped together consecutively. A crucial observation is that every valid substring is centered around a boundary between two adjacent blocks of differing characters. The number of balanced substrings formed across any adjacent blocks of lengths prevRun and currRun is simply min(prevRun, currRun).

We optimize the solution to run in a single pass without storing any auxiliary array. By maintaining only two scalar variables for the previous block length and current block length, we count consecutive characters and increment our total count at each character change and at the end of the string. This achieves optimal O(n) linear time complexity and O(1) auxiliary space complexity.

πŸ”₯ Problem Highlights

βœ… Boundary-centered counting for consecutive 0s and 1s
βœ… Computing valid substrings using min(prevRun, currRun)
βœ… On-the-fly run-length tracking eliminating array allocation
βœ… Handles alternating characters, single runs, and large strings up to 100,000 characters
βœ… Clean, production-grade Java solution
βœ… O(n) time complexity and O(1) auxiliary space complexity

⏱️ TIMESTAMPS

00:00 Intro & Problem Overview
01:17 Problem Understanding & Constraints
02:44 Consecutive Run Lengths & Boundary Transition
04:25 Single-Pass Two-Counter Approach
05:38 Java Solution Walkthrough Line by Line
06:55 Test Cases Walkthrough & Simulation
08:20 Time Complexity Analysis (O(N) Linear)
09:04 Space Complexity Analysis (O(1) Registers)
09:38 Outro & Wrap-up

πŸ“‚ Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/19-February/696-Count-Binary-Substrings

β˜• Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/19-February/696-Count-Binary-Substrings/Solution.java

🐍 Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/19-February/696-Count-Binary-Substrings/solution.py

⚑ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/19-February/696-Count-Binary-Substrings/solution.cpp

🌐 JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/19-February/696-Count-Binary-Substrings/solution.js

πŸ”— LeetCode Source Code

πŸ“Œ Admin type the link

🌐 All LeetCode Solutions

πŸ“Œ https://codebytushu.com/Leetcode/

πŸ“± Join CodeByTushu on WhatsApp

Get coding updates, LeetCode solutions, daily questions and more.

https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40

❀️ Donate

πŸ“Œ https://codebytushu.com/Leetcode/donate.php

πŸ”₯ Perfect For

LeetCode Easy Problems
Java DSA Preparation
Two Pointers Technique
String Manipulation Patterns
Consecutive Run Counting
Time Complexity Optimization
Coding Interview Preparation
Problem Solving

🌐 Follow CodeByTushu

▢️ YouTube: https://www.youtube.com/@codebytushu

πŸ“Έ Instagram: https://instagram.com/codebytushu/

πŸ’Ό LinkedIn: https://www.linkedin.com/company/codebytushu/

🌍 Website: https://codebytushu.com

πŸ™ GitHub: https://github.com/CodeByTushu

❀️ Don’t Forget To

πŸ‘ Like | πŸ’¬ Comment | πŸ”” Subscribe

πŸ’¬ Question of the Day

If two adjacent consecutive blocks of characters in a binary string have lengths 4 and 7, how many valid substrings with equal 0s and 1s can be formed between them?

1️⃣ 3
2️⃣ 4
3️⃣ 7
4️⃣ 11

#LeetCode696 #LeetCode #CountBinarySubstrings #Java #JavaDSA #TwoPointers #String #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu

πŸ‘‡ Comment the answer below! πŸ”₯Β Close description
Β Β Β Β Β Β 
Videos related to "LeetCode 696 | Count Binary Substrings | Java | Easy | Two Pointers & Group Counting | CodeByTushu"

Related videos are temporarily unavailable.

Try again, or come back later.