...

Remove All Adjacent Duplicates in String II: A Comprehensive Guide

Remove All Adjacent Duplicates in String II: A Comprehensive GuideSource: bing.com

Have you ever encountered a problem wherein you need to remove all adjacent duplicates in a string? If yes, then you have come to the right place! In this article, we will discuss the solution to this problem using different methods and explain them in a simple and easy-to-understand manner.

What is Remove All Adjacent Duplicates in String II?

Adjacent Duplicates In StringSource: bing.com

Remove All Adjacent Duplicates in String II is a common problem in computer science wherein we need to remove all adjacent duplicates in a string. In this problem, we are given a string s and an integer K. We need to remove all adjacent duplicates in the string s such that no two adjacent characters are the same and the length of the string s is minimized. Furthermore, we can only remove K adjacent duplicates at a time.

How to Solve Remove All Adjacent Duplicates in String II?

Solve Remove All Adjacent Duplicates In String IiSource: bing.com

There are several ways to solve the Remove All Adjacent Duplicates in String II problem. In this article, we will discuss three different methods:

  1. Using Stack
  2. Using Recursion
  3. Using Two-Pointers

Method 1: Using Stack

Using Stack In Remove All Adjacent Duplicates In String IiSource: bing.com

In this method, we will use a stack to remove all adjacent duplicates in the string. We will iterate through the string s and push each character onto the stack. If the top element of the stack is the same as the current character, we will remove the top element and the current character. We will repeat this process until the top element of the stack is different from the current character or the stack is empty. Finally, we will reverse the stack and return the resulting string.

Algorithm:

  1. Create an empty stack.
  2. Iterate through the string s.
  3. Push each character onto the stack.
  4. If the top element of the stack is the same as the current character, remove the top element and the current character.
  5. Repeat step 4 until the top element of the stack is different from the current character or the stack is empty.
  6. Reverse the stack and return the resulting string.

Code:

class Solution {public String removeDuplicates(String s, int k) {Stack<Character> stack = new Stack<>();Stack<Integer> countStack = new Stack<>();for(char c : s.toCharArray()) {if(!stack.isEmpty() && stack.peek() == c) {countStack.push(countStack.peek() + 1);if(countStack.peek() == k) {countStack.pop();stack.pop();}} else {stack.push(c);countStack.push(1);}}StringBuilder sb = new StringBuilder();while(!stack.isEmpty()) {sb.append(stack.pop());}return sb.reverse().toString();}}

Method 2: Using Recursion

Using Recursion In Remove All Adjacent Duplicates In String IiSource: bing.com

In this method, we will use recursion to remove all adjacent duplicates in the string. We will iterate through the string s and count the number of adjacent duplicates. If the number of adjacent duplicates is greater than or equal to k, we will call the removeDuplicates function recursively and pass the substring without the adjacent duplicates. We will repeat this process until there are no adjacent duplicates in the string.

Algorithm:

  1. Iterate through the string s.
  2. Count the number of adjacent duplicates.
  3. If the number of adjacent duplicates is greater than or equal to k, call the removeDuplicates function recursively and pass the substring without the adjacent duplicates.
  4. Repeat step 2 and 3 until there are no adjacent duplicates in the string.
  5. Return the resulting string.

Code:

class Solution {public String removeDuplicates(String s, int k) {int n = s.length();int[] count = new int[n];StringBuilder sb = new StringBuilder();for(int i = 0; i < n; i++) {sb.append(s.charAt(i));int j = sb.length() - 1;count[j] = 1 + (j > 0 && sb.charAt(j) == sb.charAt(j - 1) ? count[j - 1] : 0);if(count[j] == k) {sb.delete(j - k + 1, j + 1);}}return sb.toString();}}

Method 3: Using Two-Pointers

Using Two Pointers In Remove All Adjacent Duplicates In String IiSource: bing.com

In this method, we will use two pointers to remove all adjacent duplicates in the string. We will iterate through the string s and use two pointers i and j to keep track of the characters. If the character at i is the same as the character at j, we will increment j. Otherwise, we will check if j – i is greater than or equal to k. If yes, we will remove the adjacent duplicates and set i to j – k. We will repeat this process until there are no adjacent duplicates in the string.

Algorithm:

  1. Initialize two pointers i and j to 0.
  2. Iterate through the string s.
  3. Check if the character at i is the same as the character at j. If yes, increment j.
  4. Otherwise, check if j – i is greater than or equal to k. If yes, remove the adjacent duplicates and set i to j – k. Otherwise, increment i.
  5. Repeat step 3 to 4 until there are no adjacent duplicates in the string.
  6. Return the resulting string.

Code:

class Solution {public String removeDuplicates(String s, int k) {char[] stack = s.toCharArray();int[] count = new int[stack.length];int i = 0;for(int j = 0; j < stack.length; j++, i++) {stack[i] = stack[j];count[i] = i > 0 && stack[i - 1] == stack[j] ? count[i - 1] + 1 : 1;if(count[i] == k) {i -= k;}}return new String(stack, 0, i);}}

Conclusion

In conclusion, Remove All Adjacent Duplicates in String II is a common problem in computer science that can be solved using different methods. In this article, we have discussed three different methods – Using Stack, Using Recursion, and Using Two-Pointers – and explained them in a simple and easy-to-understand manner. We hope that this article has helped you in understanding this problem and solving it efficiently.

Related video of Remove All Adjacent Duplicates in String II: A Comprehensive Guide

Leave a Reply

Your email address will not be published. Required fields are marked *