Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. class Solution {
  2.     public List<List<Integer>> palindromePairs(String[] words) {
  3.         HashMap<String, Integer> map = new HashMap();
  4.        
  5.         for (int i = 0; i < words.length; i++){
  6.             map.put(words[i], i);
  7.         }
  8.        
  9.         List<List<Integer>> result = new ArrayList<>();
  10.        
  11.         //fucking case of the empty string
  12.         if (map.containsKey("")) {
  13.             int emptyIndex = map.get("");
  14.            
  15.             for (int i = 0; i < words.length; i++) {
  16.                 if (i != emptyIndex && isPalindrome(words[i])) {
  17.                     result.add(Arrays.asList(emptyIndex, i));
  18.                     result.add(Arrays.asList(i, emptyIndex));
  19.                 }    
  20.             }
  21.         }
  22.        
  23.         //just a direct reverse
  24.         for (int i = 0; i < words.length; i++) {
  25.             findSupplementString(map, result, words[i], i, true);
  26.         }
  27.        
  28.         //looking for left and right candidates
  29.         for (int i = 0; i < words.length; i++) {
  30.             String word = words[i];
  31.             int wordLength = word.length();
  32.             for(int j = 1; j < wordLength; j++){
  33.                 String leftPart = word.substring(0, j);
  34.                 String rightPart = word.substring(j);
  35.                
  36.                 if(isPalindrome(leftPart)){
  37.                     findSupplementString(map, result, rightPart, i, false);
  38.                 }
  39.                
  40.                 if(isPalindrome(rightPart)){
  41.                     findSupplementString(map, result, leftPart, i, true);
  42.                 }
  43.             }
  44.         }
  45.        
  46.         return result;
  47.            
  48.     }
  49.    
  50.     private void findSupplementString(HashMap<String, Integer> map, List<List<Integer>> result, String candidate,  int candidateIndex, boolean candFirst){
  51.         String reversedWord = new StringBuilder(candidate).reverse().toString();
  52.        
  53.         if (map.containsKey(reversedWord)){
  54.             int newIndex = map.get(reversedWord);
  55.             if(candidateIndex != newIndex) {
  56.                
  57.                 if(candFirst) {
  58.                     result.add(Arrays.asList(candidateIndex, newIndex));    
  59.                 }
  60.                 else
  61.                     result.add(Arrays.asList(newIndex, candidateIndex));
  62.             }
  63.         }
  64.     }
  65.    
  66.     private boolean isPalindrome(String s){
  67.         int l = 0;
  68.         int r = s.length() - 1;
  69.         char[] charArray = s.toCharArray();
  70.         while(l <= r){
  71.             if(charArray[l] != charArray[r]){
  72.                 return false;
  73.             }
  74.             l++;
  75.             r--;
  76.         }
  77.         return true;
  78.     }
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement