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"]
方法1: hash
discussion:https://leetcode.com/problems/palindrome-pairs/discuss/129865/Clean-C%2B%2B-solution
思路:
- 遍历第一遍,将所有单词的reverse存进hashmap。
- 遍历第二遍,将每个单词做所有可能的拆分,left | right,这时要检查两种情况。可能如果right是palindrome,并且left存在于hash当中,称其原型为candidate,那么left | right | candidate 就是一个回文, 对应{ i , hash[left] }。也可能如果left是palindrome,并且right存在于hash当中,那么candidate | left | right 就是一个回文,对应{ hash[right], i }。
这个方法重要的是如何处理edge case。第一种,如果单词本身就是回文怎么办?
比如“aba”, 第一种不一切到底的写法(k < words[i].size()),也就是left可能是空,但是right不可能,那么我们会去查找left = ““是否在hash, right = “aba"是否回文。这时如果hash包括了[“”],“aba” + “” 会被包含在结果内。但是此时我们同时应该计入”” + “aba”,所以第一种方法手动加入了这些edge case,i.e. {hash[”"], i },但是排除自己“” + “” 的情况。(blank && isPalindrome(words[i]) && words[i] != “”)。
如果用第二种方法,左右都一切到底,那么会出现"" + “aba” 和“aba” + “”, 确实都被包含进去了,但是因为每个单词都在左全切和右全切中出现了两次,会产生以下这种重复:“abcd” + “”找到了"bcda",“bcda” + ““又找到了“abcd”。所以第二种方法对edge的处理就是,手动去掉了这种情况的左全切情况(!left.empty()),只保留一种。那么为什么不能直接用k = 1起手呢?思考[“aba”, “”]的情况,generalize来讲,就是对于一个自身回文和一个空串的组合,答案应该包括{i, j},{j, i},且这两个情况都需要由“aba”的循环来完成。也就是说,”” + “aba"找到了“”,“aba” + “” 找到了“”,如果缺一边就会漏掉一种组合。那么综上两种edge只能采取左右全切且手动砍掉"abcd”, "dcba"的重复case。
Input:
["abcd","dcba","lls","s","sssll"]
Output:
[[1,0],[0,1],[0,1],[1,0],[3,2],[2,4]]
Expected:
[[0,1],[1,0],[3,2],[2,4]]
易错点
- edge case的处理方法
- 注意判断hash[candidate] != i:用来排除回文单词自己被误认为pair的情况。
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> result;
unordered_map<string, int> hash;
bool blank = false;
for (int i = 0; i < words.size(); i++) {
if (words[i] == "") blank = true;
string candidate = words[i];
reverse(candidate.begin(), candidate.end());
hash[candidate] = i;
}
for (int i = 0; i < words.size(); i ++) {
if (blank && isPalindrome(words[i]) && words[i] != "") {
result.push_back({hash[""], i});
}
for (int k = 0; k < words[i].size(); k++) {
string left = words[i].substr(0, k);
string right = words[i].substr(k);
if (hash.count(left) && isPalindrome(right) && hash[left] != i) {
result.push_back({i, hash[left]});
}
if (hash.count(right) && isPalindrome(left) && hash[right] != i){
result.push_back({hash[right] , i});
}
}
}
return result;
}
bool isPalindrome(string & str) {
int left = 0, right = str.size() - 1;
while (left < right) {
if (str[left++] != str[right--]) return false;
}
return true;
}
};
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> result;
unordered_map<string, int> hash;
for (int i = 0; i < words.size(); i++) {
string candidate = words[i];
reverse(candidate.begin(), candidate.end());
hash[candidate] = i;
}
for (int i = 0; i < words.size(); i ++) {
for (int k = 0; k <= words[i].size(); k++) {
string left = words[i].substr(0, k);
string right = words[i].substr(k);
if (hash.count(left) && isPalindrome(right) && hash[left] != i) {
result.push_back({i, hash[left]});
}
if (!left.empty() && hash.count(right) && isPalindrome(left) && hash[right] != i){
result.push_back({hash[right] , i});
}
}
}
return result;
}
bool isPalindrome(string & str) {
int left = 0, right = str.size() - 1;
while (left < right) {
if (str[left++] != str[right--]) return false;
}
return true;
}
};