Algorithm/Leet Code
[LeetCode/Python] 1221. Split a String in Balanced Strings
byolee
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/
Split a String in Balanced Strings - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com