Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active October 25, 2018 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/2c4c2a29c1ff9037b27b81b4033f8daf to your computer and use it in GitHub Desktop.
Save fpdjsns/2c4c2a29c1ff9037b27b81b4033f8daf to your computer and use it in GitHub Desktop.
[leetcode] 917. Reverse Only Letters : https://leetcode.com/problems/reverse-only-letters/
class Solution {
public:
bool isCharacter(char c){
return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
}
string reverseOnlyLetters(string S) {
queue<char> q;
stack<char> s;
for(int i=0;i<S.size();i++){
char now = S[i];
if(isCharacter(now))
s.push(now);
else
q.push(now);
}
string ans = "";
for(int i=0;i<S.size();i++){
if(isCharacter(S[i])){
ans += s.top();
s.pop();
}
else{
ans += q.front();
q.pop();
}
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment