[Leetcode] Remove All Adjacent Duplicates In String (c++)

Algorithm|2022. 1. 16. 12:08
반응형
 

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

 

스택을 사용해 간단하게 풀었다.

 

class Solution {
public:
    string removeDuplicates(string s) 
    {
        string ans;
        ans += s[0];
        
        for(int i=1; i<s.size(); i++)
        {

            if(ans.back() == s[i])
                ans.erase(ans.size()-1,ans.size());
            
            else
                ans += s[i];
        }
        
        return ans;
    }
};
반응형

댓글()