Java mise en œuvre du palindrome LeetCode 336

336. palindrome de

Pour définir un ensemble unique de mots, pour trouver toutes les différentes paires d'index (i, j), de sorte que les deux mots dans la liste, les mots [i] + mots [j], peut être épissé dans une séquence palindrome.

Exemple 1:

Entrée: [ "abcd", "dcba ", "LLS", "s", "sssll"]
Rendement: [[0,1], [1,0], [3,2], [2,4]]
explication: peut être épissé dans la séquence palindromique est [ "dcbaabcd", "abcddcba" , "slls", "llssssll"]
exemple 2:

Entrée: [ "bat", "tab ", "chat"]
Sortie: [[0,1], [1,0]]
Explication: peut être épissé dans la séquence palindromique est [ "battab", "Tabbat" ]

PS:
chaîne trie inverse construit, et détermine ensuite si la chaîne en cours est présent dans l'arbre dictionnaire, tels que la présence d'autres cordes prouver l'existence de sous-chaînes et la chaîne est égale à la glace, la durée moyenne de la sous - chaîne est définie k, la complexité de O (N * K ^ 2)

class Solution {
      private static List<List<Integer>> ans = new ArrayList<>();

  
    public List<List<Integer>> palindromePairs(String[] words) {
        if (words == null || words.length == 0) {
            return null;
        }

        ans = 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++) {
            find(root, words[i], i);
        }
        return ans;
    }

    private static class TrieNode {
        //当前字符串的数组位置,下游节点,以及能构成当前串or回文子串节点的数组位置集合
        int index;
        TrieNode[] next;
        List<Integer> palindIndex;

        public TrieNode() {
            index = -1;
            next = new TrieNode[26];
            palindIndex = new ArrayList<>();
        }
    }

    private static void addWord(TrieNode root, String word, int index) {
        for (int i = word.length() - 1; i >= 0; i--) {
            int ch = word.charAt(i) - 'a';
            if (root.next[ch] == null) {
                root.next[ch] = new TrieNode();
            }

            if (isPalindrome(word, 0, i)) {
                root.palindIndex.add(index);
            }
            root = root.next[ch];
        }
        root.index = index;
        root.palindIndex.add(index);
    }

    private static void find(TrieNode root, String word, int index) {
        for (int i = 0; i < word.length(); i++) {
            //待匹配串比字典树子串长,如asadcc匹配树上的asad
            if (root.index != -1 && root.index != index && isPalindrome(word, i, word.length() - 1)) {
                ans.add(Arrays.asList(index, root.index));
            }
            //System.out.println("root index:" + root.index);
            if (root.next[word.charAt(i) - 'a'] == null) {
                return;
            }
            root = root.next[word.charAt(i) - 'a'];
        }
        //待匹配串比字典树子串短,如asad匹配树上的asadcc
        for (int i : root.palindIndex) {
            if (i != index) {
                ans.add(Arrays.asList(index, i));
            }
        }
    }

    private static boolean isPalindrome(String string, int l, int r) {
        while (l < r) {
            if (string.charAt(l++) != string.charAt(r--)) {
                return false;
            }
        }
        return true;
    }
}
Publié 1450 articles originaux · louange de won 10000 + · vues 1,73 millions +

Je suppose que tu aimes

Origine blog.csdn.net/a1439775520/article/details/104737344
conseillé
Classement