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"]

  1. public class Solution {
  2.  
  3. public static boolean isPalindrome(StringBuffer sb) {
  4.  
  5. if(sb == null || sb.length() == 0) return true;
  6.  
  7. int l = 0, r = sb.length()-1;
  8.  
  9. while(l < r) {
  10. if(sb.charAt(l) != sb.charAt(r)) return false;
  11. ++l; --r;
  12. }
  13.  
  14. return true;
  15. }
  16.  
  17. public static void buildMappings(String[] words, HashMap<String, HashSet<String> > mapping) {
  18.  
  19. for(int i=0; i<words.length; ++i) {
  20.  
  21. StringBuffer sb = new StringBuffer(words[i]);
  22. HashSet<String> adj = new HashSet<String> ();
  23.  
  24. for(int j=0; j<=sb.length(); ++j) {
  25. /** partition words[i] into two parts */
  26. StringBuffer prefix = new StringBuffer(sb.substring(0, j));
  27. StringBuffer suffix = new StringBuffer(sb.substring(j));
  28.  
  29. /** check whether or not the prefix of words[i] is palindrome */
  30. /** if it does, then the prefix can be the rotated point, otherwise not*/
  31. if(isPalindrome(prefix)) {
  32. /** adj stores the strings where suffix + words[i] is palindrome */
  33. adj.add(suffix.reverse().toString());
  34. }
  35.  
  36. if(isPalindrome(suffix)) {
  37. /** adj stores the strings where words[i] + prefix is palindrome */
  38. adj.add(prefix.reverse().toString());
  39. }
  40. }
  41. /** add it to mapping */
  42. mapping.put(sb.toString(), adj);
  43.  
  44. }
  45.  
  46. HashSet<String> hs = new HashSet<String> ();
  47. for(int i=0; i<words.length; ++i) {
  48.  
  49. StringBuffer sb = new StringBuffer(words[i]);
  50.  
  51. if(isPalindrome(sb)) {
  52. hs.add(sb.toString());
  53. }
  54. }
  55.  
  56. for(int i=0; i<words.length; ++i) {
  57.  
  58. if(words[i].equals("")) {
  59. HashSet<String> adj = new HashSet<String> ();
  60. adj.addAll(hs);
  61. mapping.put(words[i], adj);
  62. }
  63. }
  64.  
  65. }
  66.  
  67. public List<List<Integer>> palindromePairs(String[] words) {
  68.  
  69. HashSet<List<Integer> > hres = new HashSet<List<Integer> > ();
  70. List<List<Integer> > res = new ArrayList<List<Integer> > ();
  71.  
  72. HashMap<String, HashSet<String> > mapping = new HashMap<String, HashSet<String> > ();
  73. buildMappings(words, mapping);
  74.  
  75. HashMap<String, Integer> dict = new HashMap<String, Integer> ();
  76. for(int i=0; i<words.length; ++i) {
  77. dict.put(words[i], i);
  78. }
  79.  
  80. Iterator iter = mapping.entrySet().iterator();
  81. while(iter.hasNext()) {
  82. HashMap.Entry entry = (HashMap.Entry) iter.next();
  83.  
  84. String str = (String) entry.getKey();
  85. int lpos = dict.get(str);
  86. //System.out.print(str + " ==> ");
  87. HashSet<String> hs = (HashSet<String>) entry.getValue();
  88. Iterator<String> itc = hs.iterator();
  89.  
  90. while(itc.hasNext()) {
  91. String s = itc.next();
  92.  
  93. //System.out.print(s + " ");
  94.  
  95. if(dict.containsKey(s)) {
  96. int rpos = dict.get(s);
  97. if(lpos != rpos) {
  98.  
  99. StringBuffer sb1 = new StringBuffer(str).append(s);
  100. if(isPalindrome(sb1)) {
  101. List<Integer> ll = new ArrayList<Integer> ();
  102. ll.add(lpos);
  103. ll.add(rpos);
  104. hres.add(ll);
  105. }
  106.  
  107. StringBuffer sb2 = new StringBuffer(s).append(str);
  108. if(isPalindrome(sb2)) {
  109. List<Integer> ll2 = new ArrayList<Integer> ();
  110. ll2.add(rpos);
  111. ll2.add(lpos);
  112. hres.add(ll2);
  113. }
  114. }
  115. }
  116.  
  117. }//System.out.println();
  118. }
  119.  
  120. res.addAll(hres);
  121.  
  122. return res;
  123. }
  124. }

leetcode@ [336] Palindrome Pairs (HashMap)的更多相关文章

  1. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  2. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  3. 336. Palindrome Pairs(can't understand)

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  4. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  5. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

  6. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  7. 【leetcode】336. Palindrome Pairs

    题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...

  8. [Leetcode] 336. Palindrome Pairs_Hard

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  9. 336 Palindrome Pairs 回文对

    给定一组独特的单词, 找出在给定列表中不同 的索引对(i, j),使得关联的两个单词,例如:words[i] + words[j]形成回文.示例 1:给定 words = ["bat&quo ...

随机推荐

  1. OSI七层协议

  2. 22.allegro中PCB打印设置[原创]

    1. -- 2. 3. 4. ----

  3. POJ 2409 Let it Bead(polay计数)

    题目链接:http://poj.org/problem?id=2409 题意:给出一个长度为m的项链,每个珠子可以用n种颜色涂色.翻转和旋转后相同的算作一种.有多少种不同的项链? 思路: (1) 对于 ...

  4. Android app Splash页的替代方案

    一般的App想要显示公司的log什么的,都会在启动的第一个页面显示,就是SplashActivity. 目前在看到一个替代SplashActivity的方案. 使用SplashActivity的时候, ...

  5. eclipse教程

    http://www.eclipse.org/downloads/eclipse-packages/http://wiki.eclipse.org/Eclipse_Articles,_Tutorial ...

  6. sscanf() 和 sprintf()的用法。

    因为感觉比较有用. 这几次比赛,用过几次,所以写个程序,总结一下. 如果用sscanf(s, "%d.%d", &a, &b); 的时候,一定要注意是否s里一定有小 ...

  7. 命名空间“System.Web”中不存在类型或命名空间名称“Script”(是缺少程序集引用吗?)

    网上有些资料说,在项目上鼠标右键,添加引用→.Net→System.Web.Entensions就可以了. 实际上很多时候在项目中的添加引用窗口上,根本找不到System.Web.Entensions ...

  8. CSS强制英文换行

    1. word-break:break-all;只对英文起作用,以字母作为换行依据 2. word-wrap:break-word; 只对英文起作用,以单词作为换行依据 3. white-space: ...

  9. 待实践三:MVC3下 路由的测试 使用 RouteDebug.dll 来测试判断路由是否符合

    在需要进行测试路由是否匹配的项目中引用    RouteDebug.dll   并且在MVC的Global.asax里面加入一段代码   //下面这行代码一定是在 RegisterRoutes(Rou ...

  10. snv的绑定,检出,同步

    svn安装  http://www.android100.org/html/201511/15/196792.html svn绑定Studio 显示svn图标 效果图