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
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
