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 strr and an integer k. Your task is to implement the remove_duplicates() function. This function will find and remove the k adjacent duplicate characters in the given strr.

Constraints

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

Input

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

str = akkkabbba
k = 2

Output

The remove_duplicates() function will return a character string, if there is a character with a count less than k in the given strr. 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 remove_duplicates(strr, k) function, wherein strr is a character string and k is the integer variable.

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