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 sorted
- leetcode풀기
- python Leetcode
- python zip_longest
- 파이썬릿코드
- 파이썬 릿코드
- 파이썬 프로그래머스
- 릿코드풀기
- LeetCode
- python priority queue
- 릿코드 풀기
- binary search
- 상가수익률계산기
- 릿코드 파이썬
- 잇츠디모
- python xor
- 파이썬릿코드풀기
- 릿코드
- python 릿코드
- 코틀린기초
- leetcode풀이
- 파이썬 알고리즘
- 파이썬 알고리즘 풀기
- python 알고리즘
- 파이썬알고리즘
- leetcode 풀기
Archives
- Today
- Total
소프트웨어에 대한 모든 것
1209. Remove All Adjacent Duplicates in String II 본문
반응형
문제)
1209. Remove All Adjacent Duplicates in String II
You are given a string s and an integer k, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and the right side of the deleted substring to concatenate together.
We repeatedly make k duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique.
Example 1:
Input: s = "abcd", k = 2
Output: "abcd"
Explanation: There's nothing to delete.
Example 2:
Input: s = "deeedbbcccbdaa", k = 3
Output: "aa"
Explanation:
First delete "eee" and "ccc", get "ddbbbdaa"
Then delete "bbb", get "dddaa"
Finally delete "ddd", get "aa"
Example 3:
Input: s = "pbbcggttciiippooaais", k = 2
Output: "ps"
Constraints:
- 1 <= s.length <= 105
- 2 <= k <= 104
- s only contains lowercase English letters.
솔루션1)
- 스택 자료구조 사용 (테트리스를 생각하면 됨)
class Solution:
def removeDuplicates(self, s: str, k: int) -> str:
# element : (char, count)
stack = [[s[0], 1]]
for c in s[1:]:
if stack and stack[-1][0] == c:
stack[-1][1] += 1
if stack[-1][1] == k:
stack.pop()
else:
stack.append([c, 1])
return ''.join([item[0] * item[1] for item in stack])
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
151. Reverse Words in a String (0) | 2022.12.17 |
---|---|
150. Evaluate Reverse Polish Notation (0) | 2022.12.17 |
2225. Find Players With Zero or One Losses (0) | 2022.12.15 |
451. Sort Characters By Frequency (0) | 2022.12.14 |
1704. Determine if String Halves Are Alike (0) | 2022.12.14 |
Comments