Untitled
unknown
plain_text
9 months ago
825 B
5
Indexable
public class Solution {
public int countBinarySubstrings(String s) {
int count = 0;
int prevGroupLength = 0;
int currentGroupLength = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
currentGroupLength++;
} else {
// Add the minimum of the previous and current group lengths
count += Math.min(prevGroupLength, currentGroupLength);
// Update previous group length and reset current group length
prevGroupLength = currentGroupLength;
currentGroupLength = 1;
}
}
// Add the last pair of groups
count += Math.min(prevGroupLength, currentGroupLength);
return count;
}
}
Editor is loading...
Leave a Comment