2
public String reverseOnlyLetters(String s) {3
// converting the string to the charArray...4
char[] ch = s.toCharArray();7
int end = s.length() - 1;9
// Storing all the english alphabets in a hashmap so that the searching becomes easy...10
HashMap<Character, Integer> hash = new HashMap<>();11
for (int i = 0; i < 26; i++) {12
hash.put((char) (97 + i), 1);14
for (int i = 0; i < 26; i++) {15
hash.put((char) (65 + i), 1);18
// using two while loops ..since the constraints are too less thats why we can prefer nested22
// interating untill start pointer reacher a good character23
while (start < end && !hash.containsKey(ch[start])) {26
// iterating untill the end pointer reaches the good character..27
while (end > start && !hash.containsKey(ch[end])) {31
// swapping the array elements..32
char temp = ch[start];40
// converting the charArray to the string again..41
String ans = new String(ch);44
// Time Complexity : O(N) (since the loops will run only till the number of charcters in the46
// Space Complexity : O(N) since we used hashmap..