题目链接:https://leetcode.com/problems/palindrome-pairs/
题目:
Given a list of unique words. Find all pairs of distinct indices (i,
in the given list, so that the concatenation of the two words, i.e.
j)words[i] + words[j]
is
a palindrome.
Example 1:
Given words
= ["bat",
"tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]
Example 2:
Given words
= ["abcd",
"dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
思路:
将words放入hashMap中,对每个word将它分为两部分C+D。
若C为回文串,则在map中找是否存在D的反转串,若有则,reverse(D)+C+D构成回文串;
若D为回文串,同理。。
要注意,若words中存在空串,空串和words中回文串组合都可以构成回文串,同时,按上述步骤要注意判断:将word分为两部分,其中非回文串是空串时,不能再在map中找对应的reverse(D)了,因为这种情况,在前面已经判断过了,避免重复判断。
算法:
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); if (!words[i].equals("") && isPalindrome(words[i])) //保存words中非空的回文word的下标 pld.add(i); } for (int i = 0; i < words.length; i++) { if (words[i].equals("")) { //如果存在空串,则空串和words中任意回文串组合都构成回文串 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); if (isPalindrome(firstPart) && dic.containsKey(reSecondPart) && !reSecondPart.equals("")) { //first是回文,second非空串,防止重复 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; }
时间: 2024-10-11 13:11:20