https://leetcode.com/problems/reverse-only-letters/
1. Two pointers:
//0 ms 6.2 MBclass Solution {public:string reverseOnlyLetters(string S) {int n = S.length();int l = 0;int r = n - 1;while(l < r){if(isalpha(S[l]) && isalpha(S[r])){ //if S[l] and S[r] are both letterschar tmp = S[l];S[l] = S[r];S[r] = tmp;l++;r--;} else if(!isalpha(S[l]) && isalpha(S[r])){l++;} else if(isalpha(S[l]) && !isalpha(S[r])){r--;} else if(!isalpha(S[l]) && !isalpha(S[r])){l++;r--;}}return S;}};
2. Two pointers(optimized):
//0 ms 6.2 MBclass Solution {public:string reverseOnlyLetters(string S) {int l = 0;int r = S.length()-1;while(l<r){if(isalpha(S[l]) && isalpha(S[r])){swap(S[l], S[r]);l++;r--;} else {if(!isalpha(S[l]))l++;if(!isalpha(S[r]))r--;}}return S;}};
