|
| 1 | +## 336. Palindrome Pairs |
| 2 | + |
| 3 | +### Question |
| 4 | +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. |
| 5 | + |
| 6 | +``` |
| 7 | +Example 1: |
| 8 | +
|
| 9 | +Input: ["abcd","dcba","lls","s","sssll"] |
| 10 | +Output: [[0,1],[1,0],[3,2],[2,4]] |
| 11 | +Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"] |
| 12 | +
|
| 13 | +Example 2: |
| 14 | +
|
| 15 | +Input: ["bat","tab","cat"] |
| 16 | +Output: [[0,1],[1,0]] |
| 17 | +Explanation: The palindromes are ["battab","tabbat"] |
| 18 | +``` |
| 19 | + |
| 20 | +### Thinking: |
| 21 | +* Method 1: Brutal force TLE |
| 22 | + ```Java |
| 23 | + class Solution { |
| 24 | + public List<List<Integer>> palindromePairs(String[] words) { |
| 25 | + int len = words.length; |
| 26 | + List<List<Integer>> res = new ArrayList<>(); |
| 27 | + for(int i = 0; i < len; i++){ |
| 28 | + for(int j = 0; j < len; j++){ |
| 29 | + if(i == j) continue; |
| 30 | + if(isPalindrome(words[i] + words[j])){ |
| 31 | + List<Integer> temp = new ArrayList<>(); |
| 32 | + temp.add(i); |
| 33 | + temp.add(j); |
| 34 | + res.add(temp); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return res; |
| 39 | + } |
| 40 | + private boolean isPalindrome(String s){ |
| 41 | + int left = 0, right = s.length() - 1; |
| 42 | + char[] arr = s.toCharArray(); |
| 43 | + while(left < right){ |
| 44 | + if(arr[left++] != arr[right--]) |
| 45 | + return false; |
| 46 | + } |
| 47 | + return true; |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +* Method 2: HashMap O(N * K^2) |
| 53 | + ```Java |
| 54 | + class Solution { |
| 55 | + public List<List<Integer>> palindromePairs(String[] words) { |
| 56 | + List<List<Integer>> res = new ArrayList<>(); |
| 57 | + if(words == null || words.length < 2) return res; |
| 58 | + Map<String, Integer> map = new HashMap<>(); |
| 59 | + for(int i = 0; i < words.length; i++){ // O(N) |
| 60 | + map.put(words[i], i); |
| 61 | + } |
| 62 | + for(int i = 0; i < words.length; i++){ // O(N * k) |
| 63 | + for(int j = 0; j <= words[i].length(); j++){ |
| 64 | + String first = words[i].substring(0, j); |
| 65 | + String second = words[i].substring(j); |
| 66 | + if(isPalindrome(first)){ // O(k) |
| 67 | + //first is palindrome, check if we have reverse of second |
| 68 | + String reverse = new StringBuilder(second).reverse().toString(); |
| 69 | + if(map.containsKey(reverse) && map.get(reverse) != i){ |
| 70 | + List<Integer> temp = new ArrayList<>(); |
| 71 | + temp.add(map.get(reverse)); |
| 72 | + temp.add(i); |
| 73 | + res.add(temp); |
| 74 | + } |
| 75 | + } |
| 76 | + if(second.length() != 0 && isPalindrome(second)){ // O(k) |
| 77 | + String reverse = new StringBuilder(first).reverse().toString(); |
| 78 | + if(map.containsKey(reverse) && map.get(reverse) != i){ |
| 79 | + List<Integer> temp = new ArrayList<>(); |
| 80 | + temp.add(i); |
| 81 | + temp.add(map.get(reverse)); |
| 82 | + res.add(temp); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return res; |
| 88 | + } |
| 89 | + private boolean isPalindrome(String s){ |
| 90 | + int left = 0, right = s.length() - 1; |
| 91 | + char[] arr = s.toCharArray(); |
| 92 | + while(left < right){ |
| 93 | + if(arr[left++] != arr[right--]) |
| 94 | + return false; |
| 95 | + } |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + ``` |
| 100 | + |
| 101 | +* Method 3: Tire Tree |
| 102 | + ```Java |
| 103 | + class Solution { |
| 104 | + private static class Node{ |
| 105 | + Node[] childs; |
| 106 | + boolean isLeaf; |
| 107 | + int index; |
| 108 | + public Node(){ |
| 109 | + this.childs = new Node[26]; |
| 110 | + } |
| 111 | + } |
| 112 | + private Node root; |
| 113 | + private void insert(String s, int index){ |
| 114 | + char[] arr = s.toCharArray(); |
| 115 | + Node temp = root; |
| 116 | + for(int i = 0; i < arr.length; i++){ |
| 117 | + if(temp.childs[arr[i] - 'a'] == null){ |
| 118 | + temp.childs[arr[i] - 'a'] = new Node(); |
| 119 | + } |
| 120 | + temp = temp.childs[arr[i] - 'a']; |
| 121 | + } |
| 122 | + temp.isLeaf = true; |
| 123 | + temp.index = index; |
| 124 | + } |
| 125 | + private Node contains(String s){ |
| 126 | + char[] arr = s.toCharArray(); |
| 127 | + Node temp = root; |
| 128 | + for(int i = 0; i < arr.length; i++){ |
| 129 | + if(temp.childs[arr[i] - 'a'] == null) return null; |
| 130 | + temp = temp.childs[arr[i] - 'a']; |
| 131 | + } |
| 132 | + return temp; |
| 133 | + } |
| 134 | + public List<List<Integer>> palindromePairs(String[] words) { |
| 135 | + this.root = new Node(); |
| 136 | + for(int i = 0; i < words.length; i++){ |
| 137 | + insert(words[i], i); |
| 138 | + } |
| 139 | + List<List<Integer>> result = new ArrayList<>(); |
| 140 | + for(int i = 0; i < words.length; i++){ |
| 141 | + for(int j = 0; j <= words[i].length(); j++){ |
| 142 | + String first = words[i].substring(0, j); |
| 143 | + String second = words[i].substring(j); |
| 144 | + if(isPalindrome(first)){ |
| 145 | + String reverse = new StringBuilder(second).reverse().toString(); |
| 146 | + Node temp = contains(reverse); |
| 147 | + if(temp != null && temp.isLeaf && temp.index != i){ |
| 148 | + result.add(Arrays.asList(temp.index, i)); |
| 149 | + } |
| 150 | + } |
| 151 | + if(second.length() != 0 && isPalindrome(second)){ |
| 152 | + String reverse = new StringBuilder(first).reverse().toString(); |
| 153 | + Node temp = contains(reverse); |
| 154 | + if(temp != null && temp.isLeaf && temp.index != i){ |
| 155 | + result.add(Arrays.asList(i, temp.index)); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + return result; |
| 161 | + } |
| 162 | + private boolean isPalindrome(String s){ |
| 163 | + int l = 0, r = s.length() - 1; |
| 164 | + while(l < r){ |
| 165 | + if(s.charAt(l++) != s.charAt(r--)) return false; |
| 166 | + } |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | + ``` |
0 commit comments