DIY: Remove All Adjacent Duplicates in String II

Solve the interview question "Remove All Adjacent Duplicates in String II" in this lesson.

We'll cover the following

Problem statement

For this problem, you are given one string str and an integer k. Your task is to implement the removeDuplicates() function. This function will find and remove the k adjacent duplicate characters in the given str.

Constraints

• $1$ <= str.length <= $10^5$
• $2$ <= k <= $10^4$
• The str only contains lowercase English letters.

Input

The removeDuplicates() function will take two inputs: str and k. The following is an example of the inputs:

str = akkkabbba
k = 2


Output

The removeDuplicates() function will return a character string, if there is a character with a count less than k in the given str. If there is no character with a count less than k, then there is no string in the output. The following is the output of the inputs given above:

akaba


Coding exercise

For this coding exercise, you need to implement the removeDuplicates(str, k) function, wherein str is a character string and k is the integer variable.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.