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:
Input: ["abcd","dcba","lls","s","sssll"] Output: [[0,1],[1,0],[3,2],[2,4]] Explanation: The palindromes are["dcbaabcd","abcddcba","slls","llssssll"]
Example 2: Input: ["bat","tab","cat"] Output: [[0,1],[1,0]] Explanation: The palindromes are["battab","tabbat"]
思路:跟two sum类似,就是存下每个word的index到hashmap里面,然后每次把word劈开两半,str1 + str2.
如果str1是palindrome,那么我们可以以str1为中间word,看有没有reverse(str2)存在,str2rvs + str1 + str2 = str2rvs + word必定是palindrome.
[i, j] , word[i] + word[j] is palindrome, 那么就加入[hashmap.get(str2rvs), i] 进入result;
如果str2 是panlindrome, 那么我们可以以str2为中间word,看有没有reverse(str1)存在,str1 + str2 + str1rvs = word + str1rvs必定是palindrome。
[i, j], , word[i] + word[j] is palindrome, 那么就加入[i, hashmap.get(str1rvs)] 进入result;
Time : O(n * wlen ^ 2); 注意:str2要判断不为空,去掉重复,因为判断str1palindrome的时候,已经把str2等于空的情况计算过了。
/**
* Step 1: store every word with its index into a hash map.
* Step 2: For each word in the array, split into two parts str1 and str2. Check whether str1 and str2 is palindrome
* If str1 is palindrome, we can use str1 as middle part, str2 as right part, and find if map contains reversed str2.
* If contains, then we can use that string as left part, combine with middle part, right part, it will form a correct
* palindrome string.
* Step 3: do all same operations for str2 (set str2 as middle part)
* */
class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> lists = new ArrayList<List<Integer>>();
HashMap<String, Integer> hashmap = new HashMap<>();
for(int i = 0; i < words.length; i++) {
hashmap.put(words[i], i);
}
// YYYYY YY YYYYY
for(int i = 0; i < words.length; i++) {
String word = words[i];
for(int j = 0; j <= word.length(); j++) {
String str1 = word.substring(0, j);
String str2 = word.substring(j);
if(isPalindrome(str1)) {
// reverseStr2 + str1 + str2;
String revstr2 = reverse(str2);
if(hashmap.containsKey(revstr2) && hashmap.get(revstr2) != i) {
List<Integer> list = new ArrayList<>();
list.add(hashmap.get(revstr2));
list.add(i);
lists.add(list);
}
}
if(str2.length() != 0 && isPalindrome(str2)) {
// str1 + str2 + revstr1;
String revstr1 = reverse(str1);
if(hashmap.containsKey(revstr1) && hashmap.get(revstr1) != i) {
List<Integer> list = new ArrayList<>();
list.add(i);
list.add(hashmap.get(revstr1));
lists.add(list);
}
}
}
}
return lists;
}
private boolean isPalindrome(String str) {
int i = 0; int j = str.length() - 1;
while(i <= j) {
if(str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
private String reverse(String str) {
StringBuilder sb = new StringBuilder();
for(int i = str.length() - 1; i >= 0; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
}
HashMap,如果速度不行,那么就用trie来存word,然后searchWord
class Solution {
class TrieNode {
public TrieNode[] children;
public int index;
public String word;
public boolean isword;
public TrieNode() {
this.children = new TrieNode[26];
this.index = -1;
this.word = null;
this.isword = false;
}
}
class Trie {
public TrieNode root;
public Trie () {
this.root = new TrieNode();
}
public void insertWord(String word, int index) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(cur.children[c - 'a'] == null) {
cur.children[c - 'a'] = new TrieNode();
}
cur = cur.children[c - 'a'];
}
cur.word = word;
cur.isword = true;
cur.index = index;
}
public int searchWord(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(cur.children[c - 'a'] == null) {
return -1;
}
cur = cur.children[c - 'a'];
}
return cur.index;
}
}
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> lists = new ArrayList<List<Integer>>();
Trie trie = new Trie();
for(int i = 0; i < words.length; i++) {
// save word;
trie.insertWord(words[i], i);
}
// YYYYY YY YYYYY
for(int i = 0; i < words.length; i++) {
String word = words[i];
for(int j = 0; j <= word.length(); j++) {
String str1 = word.substring(0, j);
String str2 = word.substring(j);
if(isPalindrome(str1)) {
// reverseStr2 + str1 + str2;
String revstr2 = reverse(str2);
int index = trie.searchWord(revstr2);
if(index != -1 && index != i) {
List<Integer> list = new ArrayList<>();
list.add(index);
list.add(i);
lists.add(list);
}
}
if(str2.length() != 0 && isPalindrome(str2)) {
// str1 + str2 + revstr1;
String revstr1 = reverse(str1);
int index = trie.searchWord(revstr1);
if(index != -1 && index != i) {
List<Integer> list = new ArrayList<>();
list.add(i);
list.add(index);
lists.add(list);
}
}
}
}
return lists;
}
private boolean isPalindrome(String str) {
int i = 0; int j = str.length() - 1;
while(i <= j) {
if(str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
private String reverse(String str) {
StringBuilder sb = new StringBuilder();
for(int i = str.length() - 1; i >= 0; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
}