1.原题:

https://leetcode.com/problems/split-a-string-in-balanced-strings/

Split a String in Balanced Strings:

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.

翻译:

对称的字段意思就是: LLRR 或者 LR 这种,现在给定一个字符串,然后查找里面有几个对称的字段。

 

2.解题思路:

这道题其实非常直白,你只需要用for loop去对照数量就行。因为字符串里面是由好几对对称的字段组成,所以你只需要记录每一次L和R数量一样的时刻就行了,因为这时候肯定是一个新的对称。

这题我因为偷懒就用了python2,实际上语法任何一个语言都行:

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

12-14 15:42