https://leetcode.com/problems/split-a-string-in-balanced-strings/python
Split a String in Balanced Strings:oop
Balanced strings are those who have equal quantity of 'L' and 'R' characters.spa
Given a balanced string s
split it in the maximum amount of balanced strings.翻譯
Return the maximum amount of splitted balanced strings.code
翻譯:leetcode
對稱的字段意思就是: LLRR 或者 LR 這種,如今給定一個字符串,而後查找裏面有幾個對稱的字段。字符串
這道題其實很是直白,你只須要用for loop去對照數量就行。由於字符串裏面是由好幾對對稱的字段組成,因此你只須要記錄每一次L和R數量同樣的時刻就好了,由於這時候確定是一個新的對稱。get
這題我由於偷懶就用了python2,實際上語法任何一個語言都行:string
class Solution(object):
def balancedStringSplit(self, s):
count_tmp1 = count_tmp2 = count_tmp3 = 0
for S in s: #檢索整個字符串
if (S == "R"):
count_tmp1 += 1 #R的數量
else:
count_tmp2 += 1 #L的數量
if (count_tmp1 == count_tmp2): #當二者數量一致的時候,說明有一個新的對稱
count_tmp3 += 1
return count_tmp3
it