g0501_0600.s0541_reverse_string_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0541_reverse_string_ii;
// #Easy #String #Two_Pointers #2022_08_02_Time_1_ms_(100.00%)_Space_45_MB_(13.41%)
/**
* 541 - Reverse String II\.
*
* Easy
*
* Given a string `s` and an integer `k`, reverse the first `k` characters for every `2k` characters counting from the start of the string.
*
* 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 left the other as original.
*
* **Example 1:**
*
* **Input:** s = "abcdefg", k = 2
*
* **Output:** "bacdfeg"
*
* **Example 2:**
*
* **Input:** s = "abcd", k = 2
*
* **Output:** "bacd"
*
* **Constraints:**
*
* * 1 <= s.length <= 104
* * `s` consists of only lowercase English letters.
* * 1 <= k <= 104
**/
public class Solution {
public String reverseStr(String s, int k) {
StringBuilder res = new StringBuilder();
int p1 = 0;
int p2 = 2 * k - 1;
if (s.length() < k) {
res.append(reverse(s));
return res.toString();
}
while (p1 < s.length()) {
if (s.length() < p1 + k) {
res.append(reverse(s.substring(p1)));
} else {
res.append(reverse(s.substring(p1, p1 + k)));
if (s.length() < p2 + 1) {
res.append(s.substring(p1 + k));
} else {
res.append(s, p1 + k, p2 + 1);
}
}
p1 = p1 + 2 * k;
p2 = p2 + 2 * k;
}
return res.toString();
}
private String reverse(String s) {
StringBuilder sb = new StringBuilder();
sb.append(s);
sb.reverse();
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy