Skip to content

Commit 792cb10

Browse files
committed
Runtime: 1 ms (Top 90.35%) | Memory: 40.8 MB (Top 87.29%)
1 parent 04194fb commit 792cb10

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
// Runtime: 1 ms (Top 90.35%) | Memory: 40.8 MB (Top 87.29%)
12
class Solution {
23
public String reverseOnlyLetters(String s) {
34
// converting the string to the charArray...
45
char[] ch = s.toCharArray();
5-
6+
67
int start = 0;
78
int end = s.length()-1;
8-
9+
910
// Storing all the english alphabets in a hashmap so that the searching becomes easy...
1011
HashMap<Character , Integer> hash = new HashMap<>();
1112
for(int i=0 ; i<26 ;i++){
@@ -14,10 +15,10 @@ public String reverseOnlyLetters(String s) {
1415
for(int i=0 ; i<26 ; i++){
1516
hash.put((char)(65+i) , 1);
1617
}
17-
18+
1819
// using two while loops ..since the constraints are too less thats why we can prefer nested loops approach..
1920
while(start<end){
20-
21+
2122
// interating untill start pointer reacher a good character
2223
while(start<end&&!hash.containsKey(ch[start])){
2324
start++;
@@ -26,21 +27,21 @@ public String reverseOnlyLetters(String s) {
2627
while(end>start&&!hash.containsKey(ch[end])){
2728
end--;
2829
}
29-
30+
3031
// swapping the array elements..
3132
char temp = ch[start];
3233
ch[start] = ch[end];
3334
ch[end] = temp;
34-
35+
3536
start++;
3637
end--;
3738
}
38-
39+
3940
// converting the charArray to the string again..
4041
String ans = new String(ch);
4142
return ans;
42-
43+
4344
// Time Complexity : O(N) (since the loops will run only till the number of charcters in the string..)
4445
// Space Complexity : O(N) since we used hashmap..
4546
}
46-
}
47+
}

0 commit comments

Comments
 (0)