【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;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值