Advertisement
ogv

Untitled

ogv
Oct 22nd, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. // Runtime: 54 ms, Your runtime beats 71.38 % of java submissions.
  2. class Solution {
  3.     public List<List<Integer>> palindromePairs(String[] words) {
  4.         TrieNode trie = new TrieNode();
  5.         for (int i = 0; i < words.length; i++) trie.add(words[i], i);        
  6.        
  7.         List<List<Integer>> result = new ArrayList<List<Integer>>();
  8.        
  9.         for (int i = 0; i < words.length; i++) {            
  10.             String word = words[i];
  11.             //System.out.println("Searching for " + word);
  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.                     //System.out.println("1st word ended " + j);
  19.                     if (node.endWord != i && isPali(word, 0, j)) {
  20.                         result.add(createResult(node.endWord, i));
  21.                     }
  22.                 }
  23.                 node = next;
  24.             }
  25.             if (node != null) {
  26.                 //System.out.println("2nd word ended");
  27.                 for (Pos pos: node.positions) {
  28.                     if (pos.wordIndex == i) continue;                                    
  29.                    
  30.                     String firstWord = words[pos.wordIndex];
  31.                     if (isPali(firstWord, pos.offset, firstWord.length() - 1)) {
  32.                          result.add(createResult(pos.wordIndex, i));
  33.                     }
  34.                 }
  35.             }            
  36.         }
  37.                
  38.         return result;
  39.     }
  40.    
  41.     private boolean isPali(String s, int l, int r) {
  42.         //System.out.println("isPali: " + s + " " + l + " " + r);
  43.         while (l < r)
  44.             if (s.charAt(l++) != s.charAt(r--)) return false;
  45.        
  46.         return true;
  47.     }
  48.    
  49.     private List<Integer> createResult(int index1, int index2) {
  50.         ArrayList<Integer> r = new ArrayList<>();
  51.         r.add(index1);
  52.         r.add(index2);
  53.         return r;
  54.     }
  55.    
  56.     class TrieNode {
  57.         public List<Pos> positions;
  58.         public Map<Character, TrieNode> next;
  59.         public int endWord;
  60.        
  61.         public TrieNode() {
  62.             next = new HashMap<Character, TrieNode>();
  63.             positions = new ArrayList<Pos>();
  64.             endWord = -1;
  65.         }
  66.        
  67.         public void add(String s, int wordIndex) {
  68.             addInt(s, 0, wordIndex);
  69.         }
  70.        
  71.         private void addInt(String s, int index, int wordIndex) {
  72.             positions.add(new Pos(wordIndex, index));
  73.             if (index >= s.length()) {
  74.                 this.endWord = wordIndex;
  75.                 return;            
  76.             }
  77.            
  78.             char c = s.charAt(index);
  79.             TrieNode n = getOrAdd(c);
  80.             n.addInt(s, index +  1, wordIndex);
  81.         }
  82.        
  83.         private TrieNode getOrAdd(char c) {
  84.             TrieNode n = next.get(c);
  85.             if (n == null) {
  86.                 n = new TrieNode();
  87.                 next.put(c, n);
  88.             }
  89.             return n;
  90.         }
  91.     }
  92.    
  93.     class Pos {
  94.         public int wordIndex;
  95.         public int offset;
  96.         public Pos(int wordIndex, int offset) {
  97.             this.wordIndex = wordIndex;
  98.             this.offset = offset;
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement