336. Palindrome Pairs

本文介绍了一种使用字典树解决寻找字符串数组中所有回文配对的问题,通过实例展示了如何构造字典树并搜索可能的回文配对,算法时间复杂度为O(n*k^2)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.

Example 1:

Input: [“abcd”,“dcba”,“lls”,“s”,“sssll”]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are [“dcbaabcd”,“abcddcba”,“slls”,“llssssll”]
Example 2:

Input: [“bat”,“tab”,“cat”]
Output: [[0,1],[1,0]]
Explanation: The palindromes are [“battab”,“tabbat”]

Problem URL


Solution

给一个word[],找到数组中所有的i,j使得word[i] + word[j]是回文。

Using Trie tree to solve this proble. Reference

Code
class Solution {
    private static class TrieNode {
        TrieNode[] next;
        int index;
        List<Integer> list;
        
        TrieNode() {
            next = new TrieNode[26];
            index = -1;
            list = new ArrayList<Integer>();
        }
    }
    
    public List<List<Integer>> palindromePairs(String[] words) {
        List<List<Integer>> res = new ArrayList<>();
        if (words == null || words.length == 0){
            return res;
        }
        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++){
            searchWord(root, words, res, i);
        }
        
        return res;
    }
    
    private void addWord(TrieNode root, String word, int index){
        for (int i = word.length() - 1; i >= 0; i--){
            int j = word.charAt(i) - 'a';
            
            if (root.next[j] == null){
                root.next[j] = new TrieNode();
            }
            
            if (isPalindrome(word, 0, i)){
                root.list.add(index);
            }
            
            root = root.next[j];
        }
        root.list.add(index);
        root.index = index;
    }
    
    private void searchWord(TrieNode root, String[] words, List<List<Integer>> res, int i){
        for (int j = 0; j < words[i].length(); j++){
            if (root.index >= 0 && root.index != i && isPalindrome(words[i], j, words[i].length() - 1)){
                res.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;
            res.add(Arrays.asList(i, j));
        }
    }
    
    private boolean isPalindrome(String word, int left, int right){
        while (left < right){
            if (word.charAt(left++) != word.charAt(right--)){
                return false;
            }
        }
        return true;
    }
}

Time Complexity: O(n * k^2)
Space Complexity: O()


Review
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值