Remove K Adjacent Duplicates
Updated
Problem statement
You are given a string s of lowercase English letters and an integer k.
One removal picks k adjacent characters of s that are all the same letter and deletes them. The parts to the left and right of them are joined, so letters that were apart can become neighbors.
Keep making removals until no k equal letters stand next to each other, and return the string that is left. The final string is the same no matter which removal you make first, so the answer is unique. It can be empty.
Function
removeDuplicates(s: String, k: int) → StringExamples
Example 1
s = "baaabbc"k = 3return = "c"Remove "aaa" to get "bbbc". Now three bs stand next to each other, so remove them as well. Only "c" is left.
Example 2
s = "aaaaa"k = 2return = "a"Each removal takes exactly two of the as: "aaaaa" becomes "aaa", then "a". One a cannot be removed on its own.
Example 3
s = "abcd"k = 2return = "abcd"No two neighbors are equal, so nothing is removed.
Example 4
s = "abbbaa"k = 3return = ""Removing "bbb" brings the first a next to the last two, which gives "aaa". Removing that leaves the empty string.
Constraints
1 <= s.length <= 1052 <= k <= 104scontains only lowercase English letters.
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Parse a Partial JSON StreamListen Labs · PHONE SCREEN
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Remove K DigitsProcol · PHONE SCREEN
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Inverse-Depth Nested List SumLinkedIn · PHONE SCREENLinkedIn interview questions
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Run a Stack Language InterpreterJane Street · PHONE SCREENJane Street interview questions
- Trace Nested Object Destruction OrderArista Networks · PHONE SCREENArista Networks interview questions