1 reverse words in a string
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification
- What constitutes a word?
A sequence of non-space characters constitutes a word.
- Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
- How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
class Solution{ public: string reverseWords(string s){ if(s.empty()){ return s; } string s_ret, s_temp; string::size_type ix = s.size(); while(ix != 0){ s_temp.clear(); while(!isspace(s[--ix])){ s_temp.push_back(s[ix]); if(ix == 0){ break; } } if(!s_temp.empty()){ if(!s_ret.empty()){ s_ret.push_back(‘ ‘); } std::reverse(s_temp.begin(), s_temp.end()); s_ret.append(s_temp); } } return s_ret; } };
2 valid palindrome
Given a string, determine if it is a palindrome,
considering only alphanumeric characters and ignoring cases.
Example
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note
Have you consider that the string might be empty?
This is a good question to ask during an interview.
For the purpose of this problem,
we define empty string as valid palindrome.
Challenge
O(n) time without extra memory.
class Solution{ public: bool isPalindrome(string& s){ if(s.empty()) return true; int l = 0, r = s.size() - 1; while(l < r){ if(!isalnum(s[l])){ ++l; continue; } if(!isalnum(s[r])){ --r; continue; } if(tolower(s[l]) == tolower(s[r])){ ++l; --r; }else{ return false; } } return true; } };
3 longest palindromic substring
Given a string S, find the longest palindromic substring in S.
You may assume that the maximum length of S is 1000,
and there exists one unique longest palindromic substring.
Example
Given the string = "abcdzdcab", return "cdzdc".
Challenge
O(n2) time is acceptable. Can you do it in O(n) time.
//题解1穷竭搜索 穷举所有可能的子串,判断子串是否为回文,使用一变量记录最大回文长度 //若新的回文超过之前的最大回文长度则更新标记变量并记录当前回文的起止索引,最后返回最长回文子串。 class Solution{ public: string longestPalindrome(string& s){ string result; if(s.empty()) return s; int n = s.size(); int longest = 0, left = 0, right = 0; for(int i = 0; i < n; ++i){ for(int j = i + 1; j <= n; ++j){ string substr = s.substr(i ,j - i); if(isPalindrome(substr) && substr.size() > longest){ longest = j - i; left = i; right = j; } } } result = s.substr(left, right - left); return result; } private: bool isPalindrome(string &s){ int n = s.size(); for(int i = 0; i < n; i++){ if(s[i] != s[n - i - 1]) return false; } return true; } };
//题解2 假定扫描的每个字母是回文的中间位置(需要处理奇偶两种情况),从该位置向两头搜索寻找最大回文长度
class Solution{ public: string palindrome(string& s, int l, int r){ while(l >= 0 && r < s.size() && s[l] == s[r]) l--, r++; return s.substr(l + 1, r - l - 1); } string longestPalindrome(string s){ if(s.empty()) return s; string res; for(int i = 0; i < s.size(); i++){ string t; t = palindrome(s, i, i); if(t.size() > res.size()) res = t; t = palindrome(s, i, i + 1); if(t.size() > res.size()) res = t; } return res; } };