close
題目:
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
Given a balanced string s
split it in the maximum amount of balanced strings.
Return the maximum amount of splitted balanced strings.
解題:
根據題目所說,字串可以拆成幾個RL數量相等的小字串
每出現一個L,計數器加一,出現一個R計數器減一
等於零時數量加一即可。
Python Code:
class Solution: def balancedStringSplit(self, s: str) -> int: res=cnt=0 for i in s: if i =="R": cnt+=1 if i =="L": cnt-=1 if cnt==0: res+=1 return res
文章標籤
全站熱搜
留言列表