-
Notifications
You must be signed in to change notification settings - Fork 0
/
145. Palindrome Pairs.cpp
39 lines (37 loc) · 1.37 KB
/
145. Palindrome Pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
vector<vector<int> > palindromePairs(vector<string>& words) {
vector<vector<int> > result;
unordered_map<string, int> dict;
int i, j, size = words.size();
string left, right, tmp;
for(i = 0; i < size; i++) {
tmp = words[i];
reverse(tmp.begin(), tmp.end());
dict[tmp] = i;
}
for(i = 0; i < size; i++) {
for(j = 0; j < words[i].size(); j++) {
left = words[i].substr(0, j);
right = words[i].substr(j);
if(dict.find(left) != dict.end() && dict[left] != i && isPalindrome(right)) {
result.push_back({i, dict[left]});
if(left.empty())
result.push_back({dict[left], i});
}
if(dict.find(right) != dict.end() && dict[right] != i && isPalindrome(left))
result.push_back({dict[right], i});
}
}
return result;
}
private:
bool isPalindrome(string s) {
int start, end, size = s.size();
for(start = 0, end = size - 1; start < end; start++, end--) {
if(s[start] != s[end])
return false;
}
return true;
}
};