-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution917.java
More file actions
38 lines (33 loc) · 1.21 KB
/
Solution917.java
File metadata and controls
38 lines (33 loc) · 1.21 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
public class Solution917 {
public static void main(String[] args) {
String s1 = "a-bC-dEf-ghIj";
String s2 = "7_28]";
String reversed = reverseOnlyLetters(s1);
System.out.println(reversed);
}
public static String reverseOnlyLetters(String S) {
if (S == null || S.length() == 0) return S;
StringBuilder sb = new StringBuilder(S);
int left = 0, right = sb.length() - 1;
while (left <= right) {
while (left < right && !Character.isLetter(sb.charAt(left))) {
left++;
}
while (right > left && !Character.isLetter(sb.charAt(right))) {
right--;
}
System.out.println("left: " + left + " " + sb.charAt(left) + ", rignt: " + right + " " + sb.charAt(right));
char charL = sb.charAt(left);
char charR = sb.charAt(right);
sb.setCharAt(right, charL);
sb.setCharAt(left, charR);
left++;
right--;
System.out.println("after exchanging: " + sb.toString());
}
return sb.toString();
}
private static boolean isLetter(char c) {
return c >= 'a' && c <= 'z';
}
}