LC 917. Reverse Only Letters

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    string reverseOnlyLetters(string S) {
        vector<char> store;
        for(int i=0;i<S.size();i++){
            if((S[i]>='a' && S[i]<='z')||(S[i]>='A' && S[i]<='Z'))
                store.push_back(S[i]);
        }
        int n = store.size()-1;
        for(int i=0;i<S.size();i++)
            if((S[i]>='a' && S[i]<='z')||(S[i]>='A' && S[i]<='Z'))
                S[i] = store[n--];
        return S;
    }
};


Dicussion Forum