【Leetcode】Palindrome Pairs

题目链接:https://leetcode.com/problems/palindrome-pairs/

题目:

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:

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

【Leetcode】Palindrome Pairs的相关文章

【LeetCode】Palindrome Pairs(336)

1. Description 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: Given words = ["bat", "tab", &qu

【LeetCode】Palindrome Partitioning II 解题报告

[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&

【LeetCode】Palindrome Partitioning 解题报告

[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",

【leetcode】Palindrome Number (easy)

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

【leetcode】Palindrome Partitioning

Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["a&qu

【LeetCode】Palindrome Partitioning II

Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome p

【LeetCode】Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restri

【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["a","a","

【LeetCode】前缀树 trie(共14题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [208]Implement Trie (Prefix Tree) (2018年11月27日) 实现基本的 trie 树,包括 insert, search, startWith 操作等 api. 题解:<程序员代码面试指南>chp5, 最后一题. 里面讲了怎么实现.这个就看代码吧.没啥好说的了. 1 class Trie { 2 public: 3 /** Ini