码迷,mamicode.com
首页 > 其他好文 > 详细

336. Palindrome Pairs

时间:2016-07-11 07:53:24      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 336. Palindrome Pairs
     * 2016-7-10 by Mingyang
     */
    class TrieNode {
        TrieNode[] next;
        int index;
        List<Integer> list;        
        TrieNode() {
            next = new TrieNode[26];
            index = -1;
            list = new ArrayList<>();
        }
    }
    public List<List<Integer>> palindromePairs(String[] words) {
        List<List<Integer>> res = new ArrayList<>();    
        TrieNode root = new TrieNode();        
        for (int i = 0; i < words.length; i++) {
            addWord(root, words[i], i);
        }        
        for (int i = 0; i < words.length; i++) {
            search(words, i, root, res);
        }        
        return res;
    }
    private void addWord(TrieNode root, String word, int index) {
        for (int i = word.length() - 1; i >= 0; i--) {
            if (root.next[word.charAt(i) - ‘a‘] == null) {
                root.next[word.charAt(i) - ‘a‘] = new TrieNode();
            }        
            if (isPalindrome(word, 0, i)) {
                root.list.add(index);
            }            
            root = root.next[word.charAt(i) - ‘a‘];
        }        
        root.list.add(index);
        root.index = index;
    }
    private void search(String[] words, int i, TrieNode root, List<List<Integer>> list) {
        for (int j = 0; j < words[i].length(); j++) {    
            if (root.index >= 0 && root.index != i && isPalindrome(words[i], j, words[i].length() - 1)) {
                list.add(Arrays.asList(i, root.index));
            }            
            root = root.next[words[i].charAt(j) - ‘a‘];
              if (root == null) return;
        }        
        for (int j : root.list) {
            if (i == j) continue;
            list.add(Arrays.asList(i, j));
        }
    }
    private boolean isPalindrome(String word, int i, int j) {
        while (i < j) {
            if (word.charAt(i++) != word.charAt(j--)) return false;
        }        
        return true;
    }

 

336. Palindrome Pairs

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5659058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!