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.
這道題給我們了許多單詞,讓我們找出回文對,就是兩個單詞拼起來是個回文字符串,我最開始嘗試的是brute force的方法,每兩個單詞都拼接起來然后判斷是否是回文字符串,但是通過不了OJ,會超時,可能這也是這道題標為Hard的原因之一吧,那么我們只能找別的方法來做,通過學習大神們的解法,發現如下兩種方法比較好,其實兩種方法的核心思想都一樣,寫法略有不同而已,那么我們先來看第一種方法吧,要用到哈希表來建立每個單詞和其位置的映射,然后需要一個set來保存出現過的單詞的長度,算法的思想是,遍歷單詞集,對於遍歷到的單詞,我們對其翻轉一下,然后在哈希表查找翻轉后的字符串是否存在,注意不能和原字符串的坐標位置相同,因為有可能一個單詞翻轉后和原單詞相等,現在我們只是處理了bat和tab的情況,還存在abcd和cba,dcb和abcd這些情況需要考慮,這就是我們為啥需要用set,由於set是自動排序的,我們可以找到當前單詞長度在set中的iterator,然后從開頭開始遍歷set,遍歷比當前單詞小的長度,比如abcdd翻轉后為ddcba,我們發現set中有長度為3的單詞,然后我們dd是否為回文串,若是,再看cba是否存在於哈希表,若存在,則說明abcdd和cba是回文對,存入結果中,對於dcb和aabcd這類的情況也是同樣處理,我們要在set里找的字符串要在遍歷到的字符串的左邊和右邊分別嘗試,看是否是回文對,這樣遍歷完單詞集,就能得到所有的回文對,參見代碼如下:
解法一:
class Solution { public: vector<vector<int>> palindromePairs(vector<string>& words) { vector<vector<int>> res; unordered_map<string, int> m; set<int> s; for (int i = 0; i < words.size(); ++i) { m[words[i]] = i; s.insert(words[i].size()); } for (int i = 0; i < words.size(); ++i) { string t = words[i]; int len = t.size(); reverse(t.begin(), t.end()); if (m.count(t) && m[t] != i) { res.push_back({i, m[t]}); } auto a = s.find(len); for (auto it = s.begin(); it != a; ++it) { int d = *it; if (isValid(t, 0, len - d - 1) && m.count(t.substr(len - d))) { res.push_back({i, m[t.substr(len - d)]}); } if (isValid(t, d, len - 1) && m.count(t.substr(0, d))) { res.push_back({m[t.substr(0, d)], i}); } } } return res; } bool isValid(string t, int left, int right) { while (left < right) { if (t[left++] != t[right--]) return false; } return true; } };
下面這種方法沒有用到set,但實際上循環的次數要比上面多,因為這種方法對於遍歷到的字符串,要驗證其所有可能的子串,看其是否在哈希表里存在,並且能否組成回文對,anyway,既然能通過OJ,說明還是比brute force要快的,參見代碼如下:
解法二:
class Solution { public: vector<vector<int>> palindromePairs(vector<string>& words) { vector<vector<int>> res; unordered_map<string, int> m; for (int i = 0; i < words.size(); ++i) m[words[i]] = i; for (int i = 0; i < words.size(); ++i) { int l = 0, r = 0; while (l <= r) { string t = words[i].substr(l, r - l); reverse(t.begin(), t.end()); if (m.count(t) && i != m[t] && isValid(words[i].substr(l == 0 ? r : 0, l == 0 ? words[i].size() - r: l))) { if (l == 0) res.push_back({i, m[t]}); else res.push_back({m[t], i}); } if (r < words[i].size()) ++r; else ++l; } } return res; } bool isValid(string t) { for (int i = 0; i < t.size() / 2; ++i) { if (t[i] != t[t.size() - 1 - i]) return false; } return true; } };
參考資料:
https://leetcode.com/discuss/91562/my-c-solution-275ms-worst-case-o-n-2
https://leetcode.com/discuss/91531/accepted-short-java-solution-using-hashmap