The title

In a balanced string, the number of ‘L’ and ‘R’ characters is the same.

You are given a balanced string s and please split it into as many balanced strings as possible.

Note: Each split string must be a balanced string.

Returns the maximum number of balanced strings that can be split.

 

Example 1: Input: s = "RLRRLLRLRL" Output: 4 Explanation: S can be split into "RL", "RRLL", "RL", "RL", "RL", each substring contains the same number of 'L' and 'R'. Example 2: Input: s = "RLLLLRRRLR" Output: 3 Explanation: S can be split into "RL", "LLLRRR", and "LR", each substring contains the same number of 'L' and 'R'. Example 3: Input: s = "LLLLRRRR" Output: 1 Description: S must be kept as "LLLLRRRR". Example 4: Input: s = "RLRRRLLRLL" Output: 2 Explanation: S can be split into "RL", "RRRLLRLL", each substring contains the same number of 'L' and 'R'.Copy the code

Tip:

1 <= s.length <= 1000 s[I] = ‘L’ or ‘R’s ‘is a balanced string

Their thinking

Class Solution: def balancedStringSplit(self, s: STR) -> int: tempList = [] ret = 0 for I in s if len(tempList) == 0 or tempList[-1] == i: tempList.append(i) elif tempList[-1] ! = i: tempList.pop() if len(tempList) == 0: ret += 1 return ret if __name__ == '__main__': s = "RLRRLLRLRL" ret = Solution().balancedStringSplit(s) print(ret)Copy the code