-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.PalindromePairs.cpp
71 lines (58 loc) · 2.1 KB
/
01.PalindromePairs.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
Example 1:
Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]
Example 2:
Input: ["bat","tab","cat"]
Output: [[0,1],[1,0]]
Explanation: The palindromes are ["battab","tabbat"]
*/
// native solution will TLE
#include "LeetcodeTools.h"
using namespace leetcode;
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
unordered_map<string, int> wordIdx;
set<int> wordLen;
vector<vector<int>> res;
for(int i = 0; i < words.size(); i++) {
wordIdx[words[i]] = i;
wordLen.insert(words[i].size());
}
for(int i = 0; i < words.size(); i++) {
string t = words[i];
reverse(t.begin(), t.end());
if(wordIdx.count(t) && wordIdx[t] != i){
res.push_back(vector<int>{i, wordIdx[t]});
}
// for each word, find all possible pair for let self as long word
for(auto it = wordLen.begin(); *it < t.size(); it++) {
int d = *it;
// self as start part
if(wordIdx.count(t.substr(t.size() - d)) && isPalindrome(t, 0, t.size() - d - 1) ) {
res.push_back(vector<int> {i, wordIdx[t.substr(t.size() - d)]});
}
// self as end part
if(wordIdx.count(t.substr(0, d)) && isPalindrome(t, d, t.size() - 1)) {
res.push_back(vector<int> {wordIdx[t.substr(0, d)], i});
}
}
}
return res;
}
private:
bool isPalindrome(string& s, int lo, int hi) {
while(lo < hi) {
if(s[lo++] != s[hi--]) return false;
}
return true;
}
};
int main() {
Solution sol;
vector<string> words{"abcd","dcba","lls","s","sssll"};
printVector2D(sol.palindromePairs(words));
}