-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromePairs.cpp
More file actions
60 lines (55 loc) · 1.97 KB
/
palindromePairs.cpp
File metadata and controls
60 lines (55 loc) · 1.97 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Source: https://leetcode.com/problems/palindrome-pairs/
// Author: Miao Zhang
// Date: 2021-02-03
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
unordered_map<string, int> wmap;
set<vector<int>> res;
for (int i = 0; i < words.size(); i++) {
wmap[words[i]] = i;
}
for (int i = 0; i < words.size(); i++) {
if (!words[i].empty() && isPalindrome(words[i]) && (wmap.find("") != wmap.end())) {
int j = wmap[""];
res.insert({i, j});
res.insert({j, i});
}
string rword = words[i];
reverse(rword.begin(), rword.end());
if (!words[i].empty() && (wmap.find(rword) != wmap.end())) {
int j = wmap[rword];
if (i != j) {
res.insert({i, j});
res.insert({j, i});
}
}
for (int j = 1; j < words[i].size(); j++) {
string left = words[i].substr(0, j);
string rleft = left;
reverse(rleft.begin(), rleft.end());
string right = words[i].substr(j);
string rright = right;
reverse(rright.begin(), rright.end());
if (isPalindrome(left) && (wmap.find(rright) != wmap.end())) {
res.insert({wmap[rright], i});
}
if (isPalindrome(right) && (wmap.find(rleft) != wmap.end())) {
res.insert({i, wmap[rleft]});
}
}
}
vector<vector<int>> ress(res.begin(), res.end());
return ress;
}
private:
bool isPalindrome(string word) {
if (word.size() <= 1) return true;
int start = 0;
int end = word.size() - 1;
while (start < end) {
if (word[start++] != word[end--]) return false;
}
return true;
}
};