Algorithm


Problem Name: 1047. 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.

 

Example 1:

Input: s = "abbaca"
Output: "ca"
Explanation: 
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

Example 2:

Input: s = "azxxzy"
Output: "ay"

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
    public String removeDuplicates(String s) {
        Stack stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (!stack.isEmpty() && stack.peek() == c) {
                stack.pop();
            } else {
                stack.push(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "abbaca"

Output

x
+
cmd
"ca"

#2 Code Example with Javascript Programming

Code - Javascript Programming


const removeDuplicates = function(S) {
  const queue = []
  for(let i = 0; i  <  S.length; i++) {
    if(queue.length > 0 && queue[queue.length - 1] === S[i]) {
       queue.pop()
    } else {
      queue.push(S[i])
    }
  }
  return queue.join('')
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "abbaca"

Output

x
+
cmd
"ca"

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def removeDuplicates(self, S: str) -> str:
        stack = []
        for s in S:
            if stack and stack[-1] == s:
                stack.pop()
            else:
                stack.append(s)
        return ''.join(stack)
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "azxxzy"

Output

x
+
cmd
"ay"

#4 Code Example with C# Programming

Code - C# Programming


using System.Text;

namespace LeetCode
{
    public class _1047_RemoveAllAdjacentDuplicatesInString
    {
        public string RemoveDuplicates(string S)
        {
            var sb = new StringBuilder();

            foreach (var ch in S)
            {
                if (sb.Length > 0 && sb[sb.Length - 1] == ch)
                    sb.Remove(sb.Length - 1, 1);
                else
                    sb.Append(ch);
            }

            return sb.ToString();
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "azxxzy"

Output

x
+
cmd
"ay"
Advertisements

Demonstration


Previous
#1046 Leetcode Last Stone Weight Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1048 Leetcode Longest String Chain Solution in C, C++, Java, JavaScript, Python, C# Leetcode