Title Link: https://leetcode.com/problems/palindrome-pairs/
Topic:
Given a list of unique words. Find all pairs of distinct indices (i, j)
in the given list, so, the concatenation of the-words, i.e. words[i] + words[j]
is a palindrome.
Example 1:
Givenwords
=["bat", "tab", "cat"]
Return[[0, 1], [1, 0]]
The palindromes is["battab", "tabbat"]
Example 2:
Givenwords
=["abcd", "dcba", "lls", "s", "sssll"]
Return[[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes is["dcbaabcd", "abcddcba", "slls", "llssssll"]
Ideas:
Put words into hashmap and divide it into two parts for each word c+d.
If C is a palindrome string, then in the map to find if there is a reversal of D string, if there is, reverse (D) +c+d constitute a palindrome string;
If D is a palindrome, the same is true.
Note that if there are empty strings in the words, empty strings and words in the combination of palindrome can constitute a palindrome string, at the same time, according to the above steps to pay attention to the judgment: Word is divided into two parts, where the non-palindrome string is empty string, can not be found in the map corresponding to reverse (D), because of this situation, have been judged before, avoid repeating judgment.
Algorithm:
Public list<list<integer>> palindromepairs (string[] words) {list<list<integer>> res = new Arraylist<list<integer>> (); map<string, integer> dic = new hashmap<string, integer> (); list<integer> PLD = new arraylist<integer> (); for (int i = 0; i < words.length; i++) {dic.put (words[i], i); I F (!words[i].equals ("") && Ispalindrome (Words[i]))//Save words the subscript of the non-empty palindrome word subscript pld.add (i);} for (int i = 0; i < words.length; i++) {if (Words[i].equals ("")) {//If there is an empty string, the combination of any palindrome string and words in the empty string constitutes a palindrome string for (int idx:pld) {list<integer> list = new arraylist<integer> (); List.add (idx); List.add (i); Res.add (List); list<integer> list2 = new arraylist<integer> () List2.add (i); List2.add (idx); Res.add (List2);}} for (int j = 0; J < Words[i].length (); j + +) {String Firstpart = words[i].substring (0, J); String Secondpart = words[i].substring (J, Words[i].length ()); String Refirstpart = reverse (Firstpart); String Resecondpart = reverse (Secondpart); (Ispalindrome (Firstpart) && Dic.containskey (Resecondpart) &&!resecondpart.equals ("")) {//first is a palindrome, second non-empty string, prevents duplicate list<integer> list = new arraylist<integer> (); if (Dic.get (resecondpart)! = i) { List.add (Dic.get (Resecondpart)); List.add (i); Res.add (list);}} if (Ispalindrome (Secondpart) && Dic.containskey (Refirstpart) &&!refirstpart.equals ("") {list< integer> list = new Arraylist<integer> (), if (Dic.get (refirstpart)! = i) {list.add (i); List.add (Dic.get ( Refirstpart)); Res.add (list);}}} return res;} public string reverse (string word) {StringBuilder sb = new StringBuilder (word); return Sb.reverse (). toString (); public boolean ispalindrome (String word) {for (int i = 0, j = word.length ()-1; i < J; i++, j--) {if (Word.charat (i)!) = Word.charat (j)) {return false;}} return true;}
"Leetcode" Palindrome Pairs