-
int longestPalindrome(string s) {
int odds = 0;
for (char c = 'A'; c <= 'z'; ++c) {
odds += count(s.begin(), s.end(), c) & 1;
}
return s.size() - max(0, odds - 1);
}
-
bool wordPattern(string pattern, string str) {
unordered_map<char, int> m1;
unordered_map<string, int> m2;
istringstream in(str);
int i = 0;
for (string word; in >> word; ++i) {
if (m1.find(pattern[i]) != m1.end() || m2.find(word) != m2.end()) {
if (m1[pattern[i]] != m2[word]) return false;
} else {
m1[pattern[i]] = m2[word] = i + 1;
}
}
return i == pattern.size();
}
-
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> m;
for (string str : strs) {
string t = str;
sort(t.begin(), t.end());
m[t].push_back(str);
}
for (auto a : m) {
res.push_back(a.second);
}
return res;
}
-
int lengthOfLongestSubstring(string s) {
int m[256] = {0}, res = 0, left = 0;
for (int i = 0; i < s.size(); ++i) {
if (m[s[i]] == 0 || m[s[i]] < left) {
res = max(res, i - left + 1);
} else {
left = m[s[i]];
}
m[s[i]] = i + 1;
}
return res;
}
-
void reversedword(char *front,char *end){
while (front<end)
{
char tmp=*front;
*front++=*end;
*end--=tmp;
}
}
char *reverse(char *s)
{
char *pre=s;
char *cur=s;
while(*cur!='\0')
if (*cur==' ')
{
reversedword(pre,cur-1);
cur++;
pre=cur;
}
else
{
cur++;
}
reversedword(pre,cur-1);
reversedword(s,cur-1);
return s;
}