Post

leetcode(리트코드)-1047 Remove All Adjacent Duplicates In String(python)

leetcode 1047 - Remove All Adjacent Duplicates In String 문제입니다.

1. 문제

https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • 연속되는 문자는 제거합니다.
  • 남는 문자열을 리턴합니다.
  • 삭제하고 난 뒤도 검사를 해줘야하므로 스택을 사용하는 것이 편합니다.

5. code

코드설명

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
    def removeDuplicates(self, s: str) -> str:
        res = ""
        st  = deque()
        for i in range(len(s)):
            if st :
                if st[-1] == s[i] : 
                    st.pop()
                else : 
                    st.append(s[i])
            else : 
                st.append(s[i])
        while st : 
            res+=st.popleft()
        return res
            
            

6. 결과 및 후기, 개선점

필요시 c++로 짜드리겠습니다.

This post is licensed under CC BY 4.0 by the author.