917. Reverse Only Letters

#### QUESTION:

Given a string `S`, return the “reversed” string where all characters that are not a letter stay in the same place, and all letters reverse their positions.

Example 1:

``````Input: "ab-cd"
Output: "dc-ba"
``````

Example 2:

``````Input: "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
``````

Example 3:

``````Input: "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
``````

Note:

1. `S.length <= 100`
2. `33 <= S[i].ASCIIcode <= 122`
3. `S` doesn’t contain `\` or `"`

#### SOLUTION:

``````class Solution {
public String reverseOnlyLetters(String S) {
char[] chars = S.toCharArray();
int start = 0;
int end = chars.length-1;
while (start<end){
while ((chars[start] < 65 || (chars[start] > 90 && chars[start] < 97) || chars[start] > 122)&&start<end) start++;
while ((chars[end] < 65 || (chars[end] > 90 && chars[end] < 97) || chars[end] > 122)&&end>start) end--;
char tmp = chars[start];
chars[start] = chars[end];
chars[end] = tmp;
start++;end--;
}
return new String(chars);
}
}
``````
>