题目链接: 点这里
使用方法: 贪心
class Solution {
public:
int balancedStringSplit(string s) {
int result = 0;
int arr[128] = {0};
for (int i = 0; i < s.length(); i++) {
arr[s[i]]++;
// 如果发现平衡字符串, 立刻分割
if (arr['L'] == arr['R']) result++, arr['L'] = arr['R'] = 0;
}
return result;
}
};
复杂度分析:
需
要
遍
历
一
般
字
符
串
,
设
字
符
串
长
度
为
n
,
那
么
时
间
复
杂
度
为
O
(
n
)
,
空
间
复
杂
度
为
O
(
1
)
需要遍历一般字符串, 设字符串长度为n, 那么时间复杂度为O(n), 空间复杂度为O(1)
需要遍历一般字符串,设字符串长度为n,那么时间复杂度为O(n),空间复杂度为O(1)