【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", "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"]
2. Answer
public class Solution { public List<List<Integer>> palindromePairs(String[] words) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(words == null || words.length == 0){ return res; } //build the map save the key-val pairs: String - idx HashMap<String, Integer> map = new HashMap<>(); for(int i = 0; i < words.length; i++){ map.put(words[i], i); } //special cases: "" can be combine with any palindrome string if(map.containsKey("")) { int blankIdx = map.get(""); for(int i = 0; i < words.length; i++) { if(isPalindrome(words[i])) { if(i == blankIdx) continue; res.add(Arrays.asList(blankIdx, i)); res.add(Arrays.asList(i, blankIdx)); } } } //find all string and reverse string pairs for(int i = 0; i < words.length; i++) { String cur_r = reverseStr(words[i]); if(map.containsKey(cur_r)) { int found = map.get(cur_r); if(found == i) continue; res.add(Arrays.asList(i, found)); } } //find the pair s1, s2 that //case1 : s1[0:cut] is palindrome and s1[cut+1:] = reverse(s2) => (s2, s1) //case2 : s1[cut+1:] is palindrome and s1[0:cut] = reverse(s2) => (s1, s2) for(int i = 0; i < words.length; i++) { String cur = words[i]; for(int cut = 1; cut < cur.length(); cut++) { if(isPalindrome(cur.substring(0, cut))) { String cut_r = reverseStr(cur.substring(cut)); if(map.containsKey(cut_r)) { int found = map.get(cut_r); if(found == i) continue; res.add(Arrays.asList(found, i)); } } if(isPalindrome(cur.substring(cut))) { String cut_r = reverseStr(cur.substring(0, cut)); if(map.containsKey(cut_r)){ int found = map.get(cut_r); if(found == i) continue; res.add(Arrays.asList(i, found)); } } } } return res; } public String reverseStr(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } public boolean isPalindrome(String s) { int i = 0; int j = s.length() - 1; while(i <= j){ if(s.charAt(i) != s.charAt(j)) { return false; } i++; j--; } return true; } }
PS:如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”,将会是我不竭的动力!
作者:leesf 掌控之中,才会成功;掌控之外,注定失败。
出处:http://www.cnblogs.com/leesf456/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果觉得本文对您有帮助,您可以请我喝杯咖啡!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!