Skip to content

Commit b66f4e1

Browse files
Added solution
1 parent 4275f31 commit b66f4e1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

reverse-only-letters.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
4+
void swap(char &x, char &y){
5+
auto t = x;
6+
x = y;
7+
y = t;
8+
}
9+
10+
string reverseOnlyLetters(string S) {
11+
12+
if(S.size() == 0) return "";
13+
14+
int i = 0, j = S.size() - 1;
15+
16+
17+
while(i <= j){
18+
19+
if(isalpha(S[i]) and isalpha(S[j]))
20+
swap(S[i], S[j]);
21+
22+
else if(!isalpha(S[i])){
23+
i++; continue;
24+
}
25+
else if(!isalpha(S[j])){
26+
j--; continue;
27+
}
28+
29+
i++; j--;
30+
}
31+
return S;
32+
}
33+
};

0 commit comments

Comments
 (0)