-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
26 lines (25 loc) · 892 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public String reverseStr(String s, int k) {
if (k < 2) return s;
StringBuilder sb = new StringBuilder();
int length = s.length(), index = 0;
while (index < length) {
if (index + 2 * k <= length) {
sb.append(reverse(s, index, index + k));
sb.append(s.substring(index + k, index + 2 * k));
index += 2 * k;
} else if (index + k <= length){
sb.append(reverse(s, index, index + k));
sb.append(s.substring(index + k));
break;
} else {
sb.append(reverse(s, index, length));
break;
}
}
return sb.toString();
}
private StringBuffer reverse(String s, int index, int end) {
return new StringBuffer(s.substring(index, end)).reverse();
}
}