-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathReverse Only Letters.java
47 lines (39 loc) · 1.54 KB
/
Reverse Only Letters.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Runtime: 1 ms (Top 90.35%) | Memory: 40.8 MB (Top 87.29%)
class Solution {
public String reverseOnlyLetters(String s) {
// converting the string to the charArray...
char[] ch = s.toCharArray();
int start = 0;
int end = s.length()-1;
// Storing all the english alphabets in a hashmap so that the searching becomes easy...
HashMap<Character , Integer> hash = new HashMap<>();
for(int i=0 ; i<26 ;i++){
hash.put((char)(97+i) , 1);
}
for(int i=0 ; i<26 ; i++){
hash.put((char)(65+i) , 1);
}
// using two while loops ..since the constraints are too less thats why we can prefer nested loops approach..
while(start<end){
// interating untill start pointer reacher a good character
while(start<end&&!hash.containsKey(ch[start])){
start++;
}
// iterating untill the end pointer reaches the good character..
while(end>start&&!hash.containsKey(ch[end])){
end--;
}
// swapping the array elements..
char temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
start++;
end--;
}
// converting the charArray to the string again..
String ans = new String(ch);
return ans;
// Time Complexity : O(N) (since the loops will run only till the number of charcters in the string..)
// Space Complexity : O(N) since we used hashmap..
}
}