파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 11. 10. 23:45

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

 

Remove All Adjacent Duplicates In String - 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

n이 10^5로 좀 큰 편이라 TL 날 줄 알면서도 쉬운 방법으로 풀어봤는데 역시나 TL ㅠㅠ

# TL 난 소스
class Solution:
    def removeDuplicates(self, s: str) -> str:
        removed = True
        while removed:
            removed = False
            for i in range(len(s)-1):
                if s[i] == s[i+1]:
                    s = s[:i] + s[i+2:]
                    removed = True
                    break
        return s

언제나처럼 솔루션을 봄.
OMG.... 스택을 활용하면 너무나 쉽게 풀리네 -_-;
솔루션을 보고 나니 진짜 간단한 문제였다는걸 알게됨 ㅠㅠㅠㅠ

class Solution:
    def removeDuplicates(self, s: str) -> str:
        st = []
        
        for c in s:
            if st and c == st[-1]:
                st.pop()
                continue
            st.append(c)
            
        return ''.join(st)
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.11.12
Today's Challenge  (0) 2022.11.11
Today's Challenge  (0) 2022.11.09
Today's Challenge  (0) 2022.11.08
Today's Challenge  (0) 2022.11.07
Comments