Advertisement
ogv

Untitled

ogv
Oct 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. // Runtime: 50 ms, faster than 72.32% of Java online submissions for Palindrome Pairs.
  2. // Memory Usage: 50.9 MB, less than 21.95% of Java online submissions for Palindrome Pairs.
  3. class Solution {
  4.     public List<List<Integer>> palindromePairs(String[] words) {
  5.         TrieNode trie = new TrieNode();
  6.         for (int i = 0; i < words.length; i++) trie.add(words[i], i);        
  7.        
  8.         List<List<Integer>> result = new ArrayList<List<Integer>>();
  9.        
  10.         for (int i = 0; i < words.length; i++) {            
  11.             String word = words[i];
  12.             TrieNode node = trie;
  13.             for (int j = word.length() - 1; j >= 0 && node != null; j--) {
  14.                 char c = word.charAt(j);
  15.                 TrieNode next = node.next.get(c);
  16.                
  17.                 if (node.endWord != -1) {
  18.                     if (node.endWord != i && isPali(word, 0, j)) {
  19.                         result.add(createResult(node.endWord, i));
  20.                     }
  21.                 }
  22.                 node = next;
  23.             }
  24.             if (node != null) {
  25.                 for (int wordIndex: node.positions) {
  26.                     if (wordIndex == i) continue;                                    
  27.                    
  28.                     result.add(createResult(wordIndex, i));                    
  29.                 }
  30.             }            
  31.         }
  32.                
  33.         return result;
  34.     }
  35.    
  36.     private boolean isPali(String s, int l, int r) {
  37.         while (l < r)
  38.             if (s.charAt(l++) != s.charAt(r--)) return false;
  39.        
  40.         return true;
  41.     }
  42.    
  43.     private List<Integer> createResult(int index1, int index2) {
  44.         ArrayList<Integer> r = new ArrayList<>();
  45.         r.add(index1);
  46.         r.add(index2);
  47.         return r;
  48.     }
  49.    
  50.     class TrieNode {
  51.         public List<Integer> positions;
  52.         public Map<Character, TrieNode> next;
  53.         public int endWord;
  54.        
  55.         public TrieNode() {
  56.             next = new HashMap<Character, TrieNode>();
  57.             positions = new ArrayList<Integer>();
  58.             endWord = -1;
  59.         }
  60.        
  61.         public void add(String s, int wordIndex) {
  62.             addInt(s, 0, wordIndex);
  63.         }
  64.        
  65.         private void addInt(String s, int index, int wordIndex) {
  66.             if (isPali(s, index, s.length() -1)) {
  67.                 positions.add(wordIndex);
  68.             }
  69.             if (index >= s.length()) {
  70.                 this.endWord = wordIndex;
  71.                 return;            
  72.             }
  73.            
  74.             char c = s.charAt(index);
  75.             TrieNode n = getOrAdd(c);
  76.             n.addInt(s, index +  1, wordIndex);
  77.         }
  78.        
  79.         private TrieNode getOrAdd(char c) {
  80.             TrieNode n = next.get(c);
  81.             if (n == null) {
  82.                 n = new TrieNode();
  83.                 next.put(c, n);
  84.             }
  85.             return n;
  86.         }
  87.     }  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement