Advertisement
vladimirVenkov

336. Palindrome Pairs Isabel2

Jun 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. public class Solution {
  2. public List<List<Integer>> palindromePairs(String[] words) {
  3.     List<List<Integer>> res = new ArrayList<List<Integer>>();
  4.     if(words == null || words.length == 0){
  5.         return res;
  6.     }
  7.     //build the map save the key-val pairs: String - idx
  8.     HashMap<String, Integer> map = new HashMap<>();
  9.     for(int i = 0; i < words.length; i++){
  10.         map.put(words[i], i);
  11.     }
  12.    
  13.     //special cases: "" can be combine with any palindrome string
  14.     if(map.containsKey("")){
  15.         int blankIdx = map.get("");
  16.         for(int i = 0; i < words.length; i++){
  17.             if(isPalindrome(words[i])){
  18.                 if(i == blankIdx) continue;
  19.                 res.add(Arrays.asList(blankIdx, i));
  20.                 res.add(Arrays.asList(i, blankIdx));
  21.             }
  22.         }
  23.     }
  24.    
  25.     //find all string and reverse string pairs
  26.     for(int i = 0; i < words.length; i++){
  27.         String cur_r = reverseStr(words[i]);
  28.         if(map.containsKey(cur_r)){
  29.             int found = map.get(cur_r);
  30.             if(found == i) continue;
  31.             res.add(Arrays.asList(i, found));
  32.         }
  33.     }
  34.    
  35.     //find the pair s1, s2 that
  36.     //case1 : s1[0:cut] is palindrome and s1[cut+1:] = reverse(s2) => (s2, s1)
  37.     //case2 : s1[cut+1:] is palindrome and s1[0:cut] = reverse(s2) => (s1, s2)
  38.     for(int i = 0; i < words.length; i++){
  39.         String cur = words[i];
  40.         for(int cut = 1; cut < cur.length(); cut++){
  41.             if(isPalindrome(cur.substring(0, cut))){
  42.                 String cut_r = reverseStr(cur.substring(cut));
  43.                 if(map.containsKey(cut_r)){
  44.                     int found = map.get(cut_r);
  45.                     if(found == i) continue;
  46.                     res.add(Arrays.asList(found, i));
  47.                 }
  48.             }
  49.             if(isPalindrome(cur.substring(cut))){
  50.                 String cut_r = reverseStr(cur.substring(0, cut));
  51.                 if(map.containsKey(cut_r)){
  52.                     int found = map.get(cut_r);
  53.                     if(found == i) continue;
  54.                     res.add(Arrays.asList(i, found));
  55.                 }
  56.             }
  57.         }
  58.     }
  59.    
  60.     return res;
  61. }
  62.  
  63. public String reverseStr(String str){
  64.     StringBuilder sb= new StringBuilder(str);
  65.     return sb.reverse().toString();
  66. }
  67.  
  68. public boolean isPalindrome(String s){
  69.     int i = 0;
  70.     int j = s.length() - 1;
  71.     while(i <= j){
  72.         if(s.charAt(i) != s.charAt(j)){
  73.             return false;
  74.         }
  75.         i++;
  76.         j--;
  77.     }
  78.     return true;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement