Loading problem...
You are given a string s consisting of lowercase English letters and a positive integer k. Your task is to repeatedly perform a k-collapse operation on the string until no more operations can be performed.
A k-collapse operation works as follows:
k adjacent identical characters in the stringAfter each removal, the concatenation may create new groups of k adjacent identical characters, which can be removed in subsequent operations. Continue performing this operation until no groups of k consecutive identical characters remain.
Return the final string after all possible k-collapse operations have been completed. The answer is guaranteed to be unique for any given input.
s = "abcd"
k = 2"abcd"No character in the string appears consecutively 2 or more times. Since there are no groups of 2 adjacent identical characters, no k-collapse operations can be performed. The string remains unchanged.
s = "deeedbbcccbdaa"
k = 3"aa"The k-collapse process works as follows: • First, identify "eee" (3 consecutive 'e's) and "ccc" (3 consecutive 'c's). Remove them to get "ddbbbdaa". • Now "bbb" forms a group of 3. Remove it to get "dddaa". • Finally, "ddd" is a group of 3. Remove it to get "aa". • No more groups of 3 exist, so the final result is "aa".
s = "pbbcggttciiippooaais"
k = 2"ps"Starting with the string, we repeatedly identify and remove pairs of adjacent identical characters: • Remove "bb", "gg", "tt", "ii", "pp", "oo", "aa" in sequence (or simultaneously where they don't overlap). • After removals and concatenations, some new pairs may form, which are also removed. • The process continues until only "ps" remains, with no adjacent duplicates.
Constraints