DIY: Remove All Adjacent Duplicates in String II

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

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

  • 11 <= str.length <= 10510^5
  • 22 <= k <= 10410^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 80+ hands-on prep courses.