• 문제 링크
Count Binary Substrings - LeetCode
Can you solve this real interview question? Count Binary Substrings - Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively
leetcode.com
• 풀이 코드
public class Solution {
public int countBinarySubstrings(String s) {
int sum = 0, prev = 0, cur = 1;
for (int i = 1, n = s.length(); i < n; i++) {
if (s.charAt(i - 1) == s.charAt(i)) cur++;
else {
sum += Math.min(prev, cur);
prev = cur;
cur = 1;
}
}
return sum + Math.min(prev, cur);
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 700. Search in a Binary Search Tree - Java (0) | 2026.09.27 |
|---|---|
| [LeetCode] 697. Degree of an Array - Java (0) | 2026.09.26 |
| [LeetCode] 693. Binary Number with Alternating Bits - Java (0) | 2026.09.24 |
| [LeetCode] 682. Baseball Game - Java (0) | 2026.09.23 |
| [LeetCode] 680. Valid Palindrome II - Java (0) | 2026.09.22 |
댓글