14

Solution: Remove All Adjacent Duplicates in String II

 3 years ago
source link: https://dev.to/seanpgallivan/solution-remove-all-adjacent-duplicates-in-string-ii-431f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Leetcode Solutions (96 Part Series)

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
};
Enter fullscreen modeExit fullscreen mode
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("")
};
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode
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])
Enter fullscreen modeExit fullscreen mode

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;
    }
}
Enter fullscreen modeExit fullscreen mode
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);
    }
}
Enter fullscreen modeExit fullscreen mode

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;
    }
};
Enter fullscreen modeExit fullscreen mode
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);
    }
};
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK