Approach
I store the lowercase and non-lowercase in two stacks, and re-assign each with reverse order by poping from the stack.
Code
class Solution {
public:
string reverseByType(string s) {
stack<char> stk1;
stack<char> stk2;
int n = s.size();
for(int i = 0 ; i < n ; ++i) {
if(s[i] >= 'a' && s[i] <= 'z') stk1.push(s[i]);
else stk2.push(s[i]);
}
for(int i = 0 ; i < n ; ++i) {
if(s[i] >= 'a' && s[i] <= 'z') {
s[i] = stk1.top();
stk1.pop();
} else {
s[i] = stk2.top();
stk2.pop();
}
}
return s;
}
};