Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- python 알고리즘
- binary search
- 파이썬 릿코드
- LeetCode
- python Leetcode
- 릿코드 파이썬
- 릿코드풀기
- python 릿코드
- leetcode풀기
- 잇츠디모
- 파이썬 알고리즘
- leetcode 풀기
- 파이썬알고리즘
- 상가수익률계산기
- python sorted
- 릿코드 풀기
- leetcode풀이
- 릿코드풀이
- 파이썬 프로그래머스
- 파이썬릿코드
- 코틀린기초
- 알고리즘풀이
- python xor
- 파이썬알고리즘풀기
- python priority queue
- 파이썬릿코드풀기
- 알고리즘풀기
- python zip_longest
- 파이썬 알고리즘 풀기
- 릿코드
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 1221. Split a String in Balanced Strings 본문
반응형
1221. Split a String in Balanced Strings
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
문제)
솔루션1)
순차적으로 'L' 문자수를 카운팅, 'R' 문자수를 카운팅해서 같아지는 시점에 balanced string 하나를 카운팅하고 'L', 'R' 문자수는 리셋 시키고 다시 반복
class Solution:
def balancedStringSplit(self, s: str) -> int:
l_count, r_count = 0, 0
res = 0
for s in s:
if s == 'L':
l_count += 1
else:
r_count += 1
if l_count == r_count:
res += 1
l_count, r_count = 0, 0
return res
솔루션2)
스택 자료구조를 사용한다. 스택이 비어있는 경우 count++, 다른 char이 나오면 stack에서 pop 수행
class Solution:
def balancedStringSplit(self, s: str) -> int:
stack = []
ret = 0
for c in s:
if len(stack) == 0:
ret += 1
stack.append(c)
elif c == stack[-1]:
stack.append(c)
else:
stack.pop()
return ret
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 1281. Subtract the Product and Sum of Digits of an Integer (0) | 2021.11.06 |
---|---|
LeetCode 풀기 - 1720. Decode XORed Array (0) | 2021.11.06 |
LeetCode 풀기 - 1302. Deepest Leaves Sum (0) | 2021.11.06 |
LeetCode 풀기 - 1979. Find Greatest Common Divisor of Array (0) | 2021.11.06 |
LeetCode 풀기 - 709. To Lower Case (0) | 2021.11.06 |
Comments