3853. Merge Close Characters Medium

@problem@discussion



1/**
2 * [3853] Merge Close Characters
3 *
4 * You are given a string s consisting of lowercase English letters and an integer k.
5 * Two equal characters in the current string s are considered close if the distance between their indices is at most k.
6 * When two characters are close, the right one merges into the left. Merges happen one at a time, and after each merge, the string updates until no more merges are possible.
7 * Return the resulting string after performing all possible merges.
8 * Note: If multiple merges are possible, always merge the pair with the smallest left index. If multiple pairs share the smallest left index, choose the pair with the smallest right index.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">s = "abca", k = 3</span>
13 * Output: <span class="example-io">"abc"</span>
14 * Explanation:
15 * 
16 * 	​​​​​​​Characters 'a' at indices i = 0 and i = 3 are close as 3 - 0 = 3 <= k.
17 * 	Merge them into the left 'a' and s = "abc".
18 * 	No other equal characters are close, so no further merges occur.
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">s = "aabca", k = 2</span>
23 * Output: <span class="example-io">"abca"</span>
24 * Explanation:
25 * 
26 * 	Characters 'a' at indices i = 0 and i = 1 are close as 1 - 0 = 1 <= k.
27 * 	Merge them into the left 'a' and s = "abca".
28 * 	Now the remaining 'a' characters at indices i = 0 and i = 3 are not close as k < 3, so no further merges occur.
29 * </div>
30 * <strong class="example">Example 3:
31 * <div class="example-block">
32 * Input: <span class="example-io">s = "yybyzybz", k = 2</span>
33 * Output: <span class="example-io">"ybzybz"</span>
34 * Explanation:
35 * 
36 * 	Characters 'y' at indices i = 0 and i = 1 are close as 1 - 0 = 1 <= k.
37 * 	Merge them into the left 'y' and s = "ybyzybz".
38 * 	Now the characters 'y' at indices i = 0 and i = 2 are close as 2 - 0 = 2 <= k.
39 * 	Merge them into the left 'y' and s = "ybzybz".
40 * 	No other equal characters are close, so no further merges occur.
41 * </div>
42 *  
43 * Constraints:
44 * 
45 * 	1 <= s.length <= 100
46 * 	1 <= k <= s.length
47 * 	s consists of lowercase English letters.
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/merge-close-characters/
53// discuss: https://leetcode.com/problems/merge-close-characters/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58    pub fn merge_characters(s: String, k: i32) -> String {
59        String::new()
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_3853() {
71    }
72}
73

Back
© 2026 bowen.ge All Rights Reserved.