Solution: Remove All Adjacent Duplicates in String II

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode’s forums.

Leetcode Problem #1209 (Medium): Remove All Adjac…


This content originally appeared on DEV Community and was authored by seanpgallivan

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.

Leetcode Problem #1209 (Medium): Remove All Adjacent Duplicates in String II

Description:


(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)

Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them causing the left and the right side of the deleted substring to concatenate together.

We repeatedly make k duplicate removals on s until we no longer can.

Return the final string after all such duplicate removals have been made.

It is guaranteed that the answer is unique.

Examples:

Example 1:
Input: s = "abcd", k = 2
Output: "abcd"
Explanation: There's nothing to delete.
Example 2:
Input: s = "deeedbbcccbdaa", k = 3
Output: "aa"
Explanation: First delete "eee" and "ccc", get "ddbbbdaa"
Then delete "bbb", get "dddaa"
Finally delete "ddd", get "aa"
Example 3:
Input: s = "pbbcggttciiippooaais", k = 2
Output: "ps"

Constraints:

  • 1 <= s.length <= 10^5
  • 2 <= k <= 10^4
  • s only contains lower case English letters.

Idea:


(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)


(Jump to: Problem Description || Solution Idea)

(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)

Idea:

Whenever we have to iterate through a data type and remove potentially nested information, the natural thought is to use some kind of stack or recursive solution to keep track of the nesting data while we search for our matches.

In a naive recursive solution, we can search for a pattern match by keeping track of the current count of adjacent duplicates, then recursively call the main function again on the string with the pattern removed. This solution repeatedly iterates through most of the string, but otherwise has low overhead, so it tends to be competitively performant, especially for shorter strings.

In an effort to achieve a more efficient solution for longer strings, we can instead use a stack to build our answer. To avoid having to backtrack up to the last K elements of our stack after removing a match, we can keep a separate stack (st) just specifically for index values of the start of each duplicate run.

To save on space, we can also use an in-place stack approach for the char array (SC) formed from the input string (S), rather than using a separate stack. To do so, we can use a two-pointer system in which one pointer (i) keeps track of the end of the in-place "stack", while the other pointer (j) iterates through SC normally.

As we move j through SC, we write to the "stack" by overwriting SC[i] with SC[j]. When we want to remove K elements from the "stack", we just move i back by K. Then, once we're done, we can return the "stack", which is the first part of SC through i.

This solution has more overhead, but won't repeat as many iterations, so it should be more efficient for longer strings.

Implementation:

C++ alone has mutable strings and doesn't require S to be split into a char array before processing as an in-place stack.

Javascript Code:


(Jump to: Problem Description || Solution Idea)

w/ Recursion:
var removeDuplicates = function(S, K) {
    for (let i = 1, count = 1; i < S.length; i++) {
        S.charAt(i) === S.charAt(i-1) ? count++ : count = 1
        if (count === K)
            S = removeDuplicates(S.slice(0, i-K+1) + S.slice(i+1), K);
    }
    return S
};
w/ In-Place Stack:
var removeDuplicates = function(S, K) {
    let SC = S.split(""), st = [0], i, j
    for (i = 1, j = 1; j < S.length; SC[++i] = SC[++j]) {
        if (SC[i] !== SC[i-1]) st.push(i)
        else if (i - st[st.length-1] + 1 === K) i = st.pop()-1
    }
    return SC.slice(0,i+1).join("")
};

Python Code:


(Jump to: Problem Description || Solution Idea)

w/ Recursion:
class Solution:
    def removeDuplicates(self, S: str, K: int) -> str:
        count, i = 1, 1
        while i < len(S):
            if S[i] == S[i-1]: count += 1
            else: count = 1
            if count == K: S = self.removeDuplicates(S[:i-K+1] + S[i+1:], K)
            i += 1
        return S
w/ In-Place Stack:
class Solution:
    def removeDuplicates(self, S: str, K: int) -> str:
        SC, st, i, j = list(S), [0], 1, 1
        while j < len(S):
            SC[i] = SC[j]
            if i == 0 or SC[i] != SC[i-1]: st.append(i)
            elif i - st[-1] + 1 == K: i = st.pop() - 1
            i += 1
            j += 1
        return "".join(SC[:i])

Java Code:


(Jump to: Problem Description || Solution Idea)

w/ Recursion:
class Solution {
    public String removeDuplicates(String S, int K) {
        for (int i = 1, count = 1; i < S.length(); i++) {
            count = S.charAt(i) == S.charAt(i-1) ? count + 1 : 1;
            if (count == K) 
                S = removeDuplicates(S.substring(0, i-K+1) + S.substring(i+1), K);
        }
        return S;
    }
}
w/ In-Place Stack:
class Solution {
    public String removeDuplicates(String S, int K) {
        char[] SC = S.toCharArray();
        int i, j;
        Stack<Integer> st = new Stack<>();
        st.add(0);
        for (i = 1, j = 1; j < S.length(); i++, j++) {
            char chr = SC[i] = SC[j];
            if (i == 0 || chr != SC[i-1]) st.add(i);
            else if (i - st.peek() + 1 == K) i = st.pop() - 1;
        }
        return new String(SC, 0, i);
    }
}

C++ Code:


(Jump to: Problem Description || Solution Idea)

w/ Recursion:
class Solution {
public:
    string removeDuplicates(string S, int K) {
        for (int i = 1, count = 1; i < S.size(); i++) {
            count = S[i] == S[i-1] ? count + 1 : 1;
            if (count == K) 
                S = removeDuplicates(S.substr(0, i-K+1) + S.substr(i+1), K);
        }
        return S;
    }
};
w/ In-Place Stack:
class Solution {
public:
    string removeDuplicates(string S, int K) {
        int i, j;
        stack<int> st;
        st.push(0);
        for (i = 1, j = 1; j < S.size(); i++, j++) {
            S[i] = S[j];
            if (i == 0 || S[i] != S[i-1]) st.push(i);
            else if (i - st.top() + 1 == K) {
                i = st.top() - 1;
                st.pop();
            }
        }
        return S.substr(0, i);
    }
};


This content originally appeared on DEV Community and was authored by seanpgallivan


Print Share Comment Cite Upload Translate Updates
APA

seanpgallivan | Sciencx (2021-04-16T12:17:29+00:00) Solution: Remove All Adjacent Duplicates in String II. Retrieved from https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/

MLA
" » Solution: Remove All Adjacent Duplicates in String II." seanpgallivan | Sciencx - Friday April 16, 2021, https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/
HARVARD
seanpgallivan | Sciencx Friday April 16, 2021 » Solution: Remove All Adjacent Duplicates in String II., viewed ,<https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/>
VANCOUVER
seanpgallivan | Sciencx - » Solution: Remove All Adjacent Duplicates in String II. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/
CHICAGO
" » Solution: Remove All Adjacent Duplicates in String II." seanpgallivan | Sciencx - Accessed . https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/
IEEE
" » Solution: Remove All Adjacent Duplicates in String II." seanpgallivan | Sciencx [Online]. Available: https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/. [Accessed: ]
rf:citation
» Solution: Remove All Adjacent Duplicates in String II | seanpgallivan | Sciencx | https://www.scien.cx/2021/04/16/solution-remove-all-adjacent-duplicates-in-string-ii/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.