반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 1047 | Remove All Adjacent Duplicates In String | Python 본문
스터디/코테
[LeetCode] 1047 | Remove All Adjacent Duplicates In String | Python
yun_s 2022. 11. 10. 09:33728x90
반응형
문제 링크: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
문제
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
조건
- 1 <= s.length <= $10^5$
- s consists of lowercase English letters.
내 풀이
[LeetCode] 1544 | Make The String Great | Python와 같은 유형의 문제지만 더 쉬운 문제다.
앞선 문제와 달리 현재 문제는 연속한 두 글자가 같은지만 확인하면 되므로 queue[-1] == char일 때만 pop을 진행한다.
코드
class Solution:
def removeDuplicates(self, s: str) -> str:
queue = []
for char in s:
if not queue:
queue.append(char)
else:
if queue[-1] == char:
queue.pop()
else:
queue.append(char)
return ''.join(queue)
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 374 | Guess Number Higher or Lower | Python (0) | 2022.11.16 |
---|---|
[LeetCode] 947 | Most Stones Removed with Same Row or Column | Python (0) | 2022.11.14 |
[LeetCode] 901 | Online Stock Span | Python (0) | 2022.11.09 |
[LeetCode] 1544 | Make The String Great | Python (0) | 2022.11.09 |
[LeetCode] 1323 | Maximum 69 Number | Python (0) | 2022.11.07 |
Comments