|
| 1 | +--- |
| 2 | +id: palindrome-pairs |
| 3 | +title: Palindrome Pairs Solution |
| 4 | +sidebar_label: 0102 Palindrome Pairs |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Trie |
| 8 | + - Hash Table |
| 9 | + - LeetCode |
| 10 | + - Java |
| 11 | + - Python |
| 12 | + - C++ |
| 13 | +description: "This is a solution to the Palindrome Pairs problem on LeetCode." |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given a list of unique words, find all pairs of distinct indices (i, j) in the list such that the concatenation of `words[i] + words[j]` forms a palindrome. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: words = ["abcd","dcba","lls","s","sssll"] |
| 26 | +Output: [[0,1],[1,0],[3,2],[2,4]] |
| 27 | +Explanation: The palindromes are ["abcddcba","dcbaabcd","slls","llssssll"] |
| 28 | +``` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: words = ["bat","tab","cat"] |
| 34 | +Output: [[0,1],[1,0]] |
| 35 | +Explanation: The palindromes are ["battab","tabbat"] |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- $1 <= words.length <= 5000$ |
| 41 | +- $0 <= words[i].length <= 300$ |
| 42 | +- words[i] consists of lowercase English letters. |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Solution for Palindrome Pairs Problem |
| 47 | + |
| 48 | +### Intuition And Approach |
| 49 | + |
| 50 | +To efficiently find all palindrome pairs in the list of words, we can leverage a HashMap (or Trie for optimization) to store the reverse of each word along with its index. Then, for each word, we check its substrings to find potential pairs that form palindromes. |
| 51 | + |
| 52 | +#### Code in Different Languages |
| 53 | + |
| 54 | +<Tabs> |
| 55 | + <TabItem value="Java" label="Java" default> |
| 56 | + <SolutionAuthor name="@mahek0620"/> |
| 57 | + ```java |
| 58 | + |
| 59 | + import java.util.*; |
| 60 | + |
| 61 | + class Solution { |
| 62 | + public List<List<Integer>> palindromePairs(String[] words) { |
| 63 | + Map<String, Integer> wordMap = new HashMap<>(); |
| 64 | + List<List<Integer>> result = new ArrayList<>(); |
| 65 | + |
| 66 | + // Build the word map |
| 67 | + for (int i = 0; i < words.length; i++) { |
| 68 | + wordMap.put(words[i], i); |
| 69 | + } |
| 70 | + |
| 71 | + // Iterate through each word |
| 72 | + for (int i = 0; i < words.length; i++) { |
| 73 | + String word = words[i]; |
| 74 | + int n = word.length(); |
| 75 | + |
| 76 | + // Check each possible split of the word into left and right parts |
| 77 | + for (int j = 0; j <= n; j++) { |
| 78 | + String left = word.substring(0, j); |
| 79 | + String right = word.substring(j); |
| 80 | + String revLeft = new StringBuilder(left).reverse().toString(); |
| 81 | + String revRight = new StringBuilder(right).reverse().toString(); |
| 82 | + |
| 83 | + // Check if left + right forms a palindrome |
| 84 | + if (isPalindrome(left) && wordMap.containsKey(revRight) && wordMap.get(revRight) != i) { |
| 85 | + result.add(Arrays.asList(wordMap.get(revRight), i)); |
| 86 | + } |
| 87 | + |
| 88 | + // Check if right + left forms a palindrome (to avoid duplicates) |
| 89 | + if (j < n && isPalindrome(right) && wordMap.containsKey(revLeft) && wordMap.get(revLeft) != i) { |
| 90 | + result.add(Arrays.asList(i, wordMap.get(revLeft))); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + private boolean isPalindrome(String s) { |
| 99 | + int left = 0, right = s.length() - 1; |
| 100 | + while (left < right) { |
| 101 | + if (s.charAt(left) != s.charAt(right)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + left++; |
| 105 | + right--; |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + ``` |
| 112 | + |
| 113 | + </TabItem> |
| 114 | + <TabItem value="Python" label="Python"> |
| 115 | + <SolutionAuthor name="@mahek0620"/> |
| 116 | + ```python |
| 117 | + |
| 118 | + from collections import defaultdict |
| 119 | + |
| 120 | + class Solution: |
| 121 | + def palindromePairs(self, words): |
| 122 | + def is_palindrome(word): |
| 123 | + return word == word[::-1] |
| 124 | + |
| 125 | + word_map = {word: i for i, word in enumerate(words)} |
| 126 | + result = [] |
| 127 | + |
| 128 | + for i, word in enumerate(words): |
| 129 | + n = len(word) |
| 130 | + for j in range(n + 1): |
| 131 | + left, right = word[:j], word[j:] |
| 132 | + rev_left, rev_right = left[::-1], right[::-1] |
| 133 | + |
| 134 | + if is_palindrome(left) and rev_right in word_map and word_map[rev_right] != i: |
| 135 | + result.append([word_map[rev_right], i]) |
| 136 | + |
| 137 | + if j < n and is_palindrome(right) and rev_left in word_map and word_map[rev_left] != i: |
| 138 | + result.append([i, word_map[rev_left]]) |
| 139 | + |
| 140 | + return result |
| 141 | + |
| 142 | + ``` |
| 143 | + |
| 144 | + </TabItem> |
| 145 | + <TabItem value="C++" label="C++"> |
| 146 | + <SolutionAuthor name="@mahek0620"/> |
| 147 | + ```cpp |
| 148 | + |
| 149 | + #include <vector> |
| 150 | + #include <unordered_map> |
| 151 | + #include <string> |
| 152 | + #include <algorithm> |
| 153 | + |
| 154 | + using namespace std; |
| 155 | + |
| 156 | + class Solution { |
| 157 | + public: |
| 158 | + vector<vector<int>> palindromePairs(vector<string>& words) { |
| 159 | + unordered_map<string, int> wordMap; |
| 160 | + vector<vector<int>> result; |
| 161 | + |
| 162 | + // Build the word map |
| 163 | + for (int i = 0; i < words.size(); ++i) { |
| 164 | + wordMap[words[i]] = i; |
| 165 | + } |
| 166 | + |
| 167 | + // Iterate through each word |
| 168 | + for (int i = 0; i < words.size(); ++i) { |
| 169 | + const string& word = words[i]; |
| 170 | + int n = word.size(); |
| 171 | + |
| 172 | + // Check each possible split of the word into left and right parts |
| 173 | + for (int j = 0; j <= n; ++j) { |
| 174 | + string left = word.substr(0, j); |
| 175 | + string right = word.substr(j); |
| 176 | + string revLeft = left; |
| 177 | + string revRight = right; |
| 178 | + reverse(revLeft.begin(), revLeft.end()); |
| 179 | + reverse(revRight.begin(), revRight.end()); |
| 180 | + |
| 181 | + // Check if left + right forms a palindrome |
| 182 | + if (isPalindrome(left) && wordMap.find(revRight) != wordMap.end() && wordMap[revRight] != i) { |
| 183 | + result.push_back({wordMap[revRight], i}); |
| 184 | + } |
| 185 | + |
| 186 | + // Check if right + left forms a palindrome (to avoid duplicates) |
| 187 | + if (j < n && isPalindrome(right) && wordMap.find(revLeft) != wordMap.end() && wordMap[revLeft] != i) { |
| 188 | + result.push_back({i, wordMap[revLeft]}); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return result; |
| 194 | + } |
| 195 | + |
| 196 | + bool isPalindrome(const string& s) { |
| 197 | + int left = 0, right = s.size() - 1; |
| 198 | + while (left < right) { |
| 199 | + if (s[left] != s[right]) { |
| 200 | + return false; |
| 201 | + } |
| 202 | + ++left; |
| 203 | + --right; |
| 204 | + } |
| 205 | + return true; |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + ``` |
| 210 | + |
| 211 | + </TabItem> |
| 212 | +</Tabs> |
| 213 | + |
| 214 | + |
| 215 | +## References |
| 216 | + |
| 217 | +- **LeetCode Problem:** [Palindrome Pairs Problem](https://leetcode.com/problems/palindrome-pairs/) |
| 218 | +- **Solution Link:** [Palindrome Pairs Solution on LeetCode](https://leetcode.com/problems/palindrome-pairs/solutions/5016750/palindrome-pairs/) |
| 219 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
| 220 | + |
| 221 | +--- |
0 commit comments