forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_917.java
24 lines (23 loc) · 789 Bytes
/
_917.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
package com.fishercoder.solutions;
public class _917 {
public static class Solution1 {
public String reverseOnlyLetters(String S) {
char[] array = S.toCharArray();
for (int i = 0, j = array.length - 1; i < j; ) {
if (Character.isLetter(array[i]) && Character.isLetter(array[j])) {
char temp = array[i];
array[i++] = array[j];
array[j--] = temp;
} else if (Character.isLetter(array[i])) {
j--;
} else if (Character.isLetter(array[j])) {
i++;
} else {
i++;
j--;
}
}
return new String(array);
}
}
}