1. Description
Given a string s and an integer k, delete any block of k identical consecutive characters, joining the remaining parts together.
Continue deleting such blocks until none remain. Return the resulting string, which is guaranteed to be unique.
2. Example
Example 1
Input: s = “abcd”, k = 2
Output: “abcd”
Explanation: No adjacent characters form a removable pair.
Example 2
Input: s = “deeedbbcccbdaa”, k = 3
Output: “aa”
Explanation: Removing “eee” and “ccc” leaves “ddbbbdaa”. Delete “bbb” to get “dddaa”, then delete “ddd” to leave “aa”.
Example 3
Input: s = “pbbcggttciiippooaais”, k = 2
Output: “ps”
Explanation: Repeatedly deleting adjacent equal pairs leaves only “ps”.
3. Constraints
- 1 <= s.length <= $10^5$
- 2 <= k <= $10^4$
- s contains only lowercase English letters.
4. Solutions
Stack with Character Counts
n = str.size()
Time complexity: O(n)
Space complexity: O(n)
| |