-
[LeetCode/Python] 1221. Split a String in Balanced StringsAlgorithm/Leet Code 2020. 7. 12. 00:47
1221. Split a String in Balanced Strings
Easy
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.
class Solution: def balancedStringSplit(self, s: str) -> int: length = len(s) result = 0 p = 0 for i in range(length) : if s[p:i+1].count('L') == s[p:i+1].count('R') : result += 1 p = i+1 return result
문자열 s를 1개씩 순회하면서 p ~ i 까지의 문자열에서 L의 개수와 R의 개수가 같으면 result를 1씩 증가하고 p를 i+1로 바꿔준다.
count 함수 개꿀
https://leetcode.com/problems/split-a-string-in-balanced-strings/
'Algorithm > Leet Code' 카테고리의 다른 글
[LeetCode/Python] 2. Add Two Numbers (0) 2020.07.15 [LeetCode/Python] 700. Search in a Binary Search Tree (0) 2020.07.15 [LeetCode/Python] 859. Buddy Strings (0) 2020.07.12