You are given a string s consisting of lowercase English letters and an integer k.
Two equal characters s[i] and s[j], where 0 <= i < j < s.length, are considered close if j - i <= k. All indices refer to the current string.
Repeatedly perform the following operation until no close pair remains:
(i, j), choose the pair with the smallest i. If multiple pairs have the same i, choose the one with the smallest j.s[j] from s. The character s[i] remains unchanged, and the remaining characters are reindexed.Return the resulting string after performing all possible merges.
Example 1:
Input: s = "abca", k = 3
Output: "abc"
Explanation:
'a' at indices 0 and 3 are close because 3 - 0 = 3 <= k.'a', resulting in s = "abc".Example 2:
Input: s = "aabca", k = 2
Output: "abca"
Explanation:
'a' at indices 0 and 1 are close because 1 - 0 = 1 <= k.'a', resulting in s = "abca".'a' characters are at indices 0 and 3. Since 3 - 0 = 3 > k, no further merges are performed.Example 3:
Input: s = "yybyzybz", k = 2
Output: "ybzybz"
Explanation:
'y' at indices 0 and 1 are close because 1 - 0 = 1 <= k. This pair has the smallest left index among all close pairs.'y', resulting in s = "ybyzybz".'y' at indices 0 and 2 are now close because 2 - 0 = 2 <= k.'y', resulting in s = "ybzybz".Constraints:
1 <= s.length <= 5 * 1051 <= k <= s.lengths consists of lowercase English letters.Loading editor...
No test cases available.