forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalindromePairs.cpp
78 lines (66 loc) · 2.64 KB
/
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
72
73
74
75
76
77
// Source : https://leetcode.com/problems/palindrome-pairs/
// Author : Hao Chen
// Date : 2017-03-22
/***************************************************************************************
*
* 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:
* Given words = ["bat", "tab", "cat"]
* Return [[0, 1], [1, 0]]
* The palindromes are ["battab", "tabbat"]
*
* Example 2:
* Given words = ["abcd", "dcba", "lls", "s", "sssll"]
* Return [[0, 1], [1, 0], [3, 2], [2, 4]]
* The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
*
* Credits:Special thanks to @dietpepsi for adding this problem and creating all test
* cases.
*
***************************************************************************************/
class Solution {
public:
bool isPalindrome(string& str) {
int left = 0, right = str.size() - 1;
while( left < right) {
if (str[left++] != str[right--]) return false;
}
return true;
}
vector<vector<int>> palindromePairs(vector<string>& words) {
unordered_map<string, int> dict;
for(int i=0; i<words.size(); i++){
string w = words[i];
reverse(w.begin(), w.end());
dict[w] = i;
}
vector<vector<int>> result;
//egde case: deal with empty string
if ( dict.find("") != dict.end() ) {
for(int i=0; i<words.size(); i++) {
if ( isPalindrome(words[i]) && dict[""] != i ) {
result.push_back( { dict[""], i } );
}
}
}
for(int i=0; i<words.size(); i++) {
for (int j=0; j<words[i].size(); j++) {
//split the word to 2 parts
string left = words[i].substr(0, j);
string right = words[i].substr(j, words[i].size() - j);
// if the `left` is found, which means there is a words has reversed sequence with it.
// then we can check the `right` part is Palindrome or not.
if ( dict.find(left) != dict.end() && isPalindrome(right) && dict[left] != i ) {
result.push_back( { i, dict[left] } );
}
if (dict.find(right) != dict.end() && isPalindrome(left) && dict[right] != i ) {
result.push_back( { dict[right], i } );
}
}
}
return result;
}
};