Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created April 23, 2021 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/3ff4d61689bc5f50a88bb768d05b8813 to your computer and use it in GitHub Desktop.
Save vrat28/3ff4d61689bc5f50a88bb768d05b8813 to your computer and use it in GitHub Desktop.
Count Binary Substrings (Java)
class Solution {
public int countBinarySubstrings(String s) {
int ans = 0, prev = 0, cur = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i-1) != s.charAt(i)) {
ans += Math.min(prev, cur);
prev = cur;
cur = 1;
} else {
cur++;
}
}
return ans + Math.min(prev, cur);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment