반응형
R과 L로 구성된 문자열을 R과 L의 개수가 똑같은 문자열로 나누려고 한다.
가장 많이 쪼갰을 때 그 숫자를 리턴하는 문제.
살짝 헷갈릴 수도 있지만
그냥 앞에서부터 R, L 개수를 각각 세면서 둘이 같아질 때를 리턴하면 된다.
class Solution {
public:
int balancedStringSplit(string s) {
int ans= 0;
int cntR= 0;
int cntL= 0;
for(int i = 0;i<s.size();i++){
if(s[i]=='R'){
cntR++;
if(cntR == cntL)
ans++;
}
else{
cntL++;
if(cntR == cntL)
ans++;
}
}
return ans;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
16] Leetcode 1252. Cells with Odd Values in a Matrix (0) | 2020.06.27 |
---|---|
15] Leetcode 1464. Maximum Product of Two Elements in an Array (0) | 2020.06.25 |
13] Leetcode 709. To Lower Case (0) | 2020.06.25 |
12] Leetcode 1486. XOR Operation in an Array (0) | 2020.06.24 |
11] Leetcode 1295. Find Numbers with Even Number of Digits (0) | 2020.06.24 |
댓글