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 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

  • 11 <= strr.length <= 10510^5
  • 22 <= k <= 10410^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.