Created
April 23, 2021 11:34
-
-
Save vrat28/3ff4d61689bc5f50a88bb768d05b8813 to your computer and use it in GitHub Desktop.
Count Binary Substrings (Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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