class Solution:
def palindromePairs(self, words):
def is_palindrome(check):
return check == check[::-1]
word_to_index = {w: i for i, w in enumerate(words)}
res = []
for i, word in enumerate(words):
for j in range(len(word) + 1):
prefix = word[:j]
suffix = word[j:]
if is_palindrome(prefix):
back = suffix[::-1]
if back != word and back in word_to_index:
res.append([word_to_index[back], i])
if j != len(word) and is_palindrome(suffix):
back = prefix[::-1]
if back != word and back in word_to_index:
res.append([i, word_to_index[back]])
return res
class Solution {
public:
bool isPalindrome(const string& s) {
int l = 0, r = s.size() - 1;
while (l < r) {
if (s[l++] != s[r--]) return false;
}
return true;
}
vector<vector<int>> palindromePairs(vector<string>& words) {
unordered_map<string, int> wordToIndex;
for (int i = 0; i < words.size(); ++i) {
wordToIndex[words[i]] = i;
}
vector<vector<int>> res;
for (int i = 0; i < words.size(); ++i) {
string word = words[i];
for (int j = 0; j <= word.size(); ++j) {
string prefix = word.substr(0, j);
string suffix = word.substr(j);
if (isPalindrome(prefix)) {
string back = string(suffix.rbegin(), suffix.rend());
if (wordToIndex.count(back) && wordToIndex[back] != i) {
res.push_back({wordToIndex[back], i});
}
}
if (j != word.size() && isPalindrome(suffix)) {
string back = string(prefix.rbegin(), prefix.rend());
if (wordToIndex.count(back) && wordToIndex[back] != i) {
res.push_back({i, wordToIndex[back]});
}
}
}
}
return res;
}
};
class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
Map<String, Integer> wordToIndex = new HashMap<>();
for (int i = 0; i < words.length; ++i) {
wordToIndex.put(words[i], i);
}
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < words.length; ++i) {
String word = words[i];
for (int j = 0; j <= word.length(); ++j) {
String prefix = word.substring(0, j);
String suffix = word.substring(j);
if (isPalindrome(prefix)) {
String back = new StringBuilder(suffix).reverse().toString();
if (wordToIndex.containsKey(back) && wordToIndex.get(back) != i) {
res.add(Arrays.asList(wordToIndex.get(back), i));
}
}
if (j != word.length() && isPalindrome(suffix)) {
String back = new StringBuilder(prefix).reverse().toString();
if (wordToIndex.containsKey(back) && wordToIndex.get(back) != i) {
res.add(Arrays.asList(i, wordToIndex.get(back)));
}
}
}
}
return res;
}
private boolean isPalindrome(String s) {
int l = 0, r = s.length() - 1;
while (l < r) {
if (s.charAt(l++) != s.charAt(r--)) return false;
}
return true;
}
}
var palindromePairs = function(words) {
const wordToIndex = {};
for (let i = 0; i < words.length; ++i) {
wordToIndex[words[i]] = i;
}
const isPalindrome = s => s === s.split('').reverse().join('');
const res = [];
for (let i = 0; i < words.length; ++i) {
let word = words[i];
for (let j = 0; j <= word.length; ++j) {
let prefix = word.substring(0, j);
let suffix = word.substring(j);
if (isPalindrome(prefix)) {
let back = suffix.split('').reverse().join('');
if (wordToIndex.hasOwnProperty(back) && wordToIndex[back] !== i) {
res.push([wordToIndex[back], i]);
}
}
if (j !== word.length && isPalindrome(suffix)) {
let back = prefix.split('').reverse().join('');
if (wordToIndex.hasOwnProperty(back) && wordToIndex[back] !== i) {
res.push([i, wordToIndex[back]]);
}
}
}
}
return res;
};
words
, find all unique pairs of indices (i, j)
such that the concatenation of words[i] + words[j]
is a palindrome. Each word can only be used at its own index, and pairs must use different indices. Return all such index pairs in any order.
word_to_index
) mapping each word to its index for O(1) lookups.
reversed_suffix
, current word) forms a palindrome.reversed_prefix
) forms a palindrome.words = ["abcd", "dcba", "lls", "s", "sssll"]