541. Reverse String II Easy

@problem@discussion
#Two Pointers#String



1/**
2 * [541] Reverse String II
3 *
4 * Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
5 * If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
6 *  
7 * Example 1:
8 * Input: s = "abcdefg", k = 2
9 * Output: "bacdfeg"
10 * Example 2:
11 * Input: s = "abcd", k = 2
12 * Output: "bacd"
13 *  
14 * Constraints:
15 * 
16 * 	1 <= s.length <= 10^4
17 * 	s consists of only lowercase English letters.
18 * 	1 <= k <= 10^4
19 * 
20 */
21pub struct Solution {}
22
23// problem: https://leetcode.com/problems/reverse-string-ii/
24// discuss: https://leetcode.com/problems/reverse-string-ii/discuss/?currentPage=1&orderBy=most_votes&query=
25
26// submission codes start here
27
28impl Solution {
29    pub fn reverse_str(s: String, k: i32) -> String {
30        String::new()
31    }
32}
33
34// submission codes end
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_541() {
42    }
43}
44


Back
© 2025 bowen.ge All Rights Reserved.