g0601_0700.s0696_count_binary_substrings.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0601_0700.s0696_count_binary_substrings;
// #Easy #String #Two_Pointers
public class Solution {
public int countBinarySubstrings(String s) {
int start = 0;
int ans = 0;
char[] arr = s.toCharArray();
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
ans++;
start = i - 1;
} else if (start > 0 && arr[--start] != arr[i]) {
// if start isn't 0, we may still have a valid substring
ans++;
} else {
// if not, then reset start to 0
start = 0;
}
}
return ans;
}
}