魔法师 (@Constanline)Leetcode每日一题 —— 3093. 最长公共后缀查询 中发帖

思路 
多亏佬友前几天题目中出现的前缀树算法,用这个答题简单多了。 
因为要 最长公共后缀中字符串最短的,多个字符串长度相同时取最最先出现的,所以前缀树的结构要加上 最短长度minLen和 最早出现idx。 
然后将 wordsContainer 中的字符串倒序插入到前缀树中,遍历 wordsQuery 用同样的找到最长公共后缀的node,取出idx即可。 
代码 
class Solution {
    private static class TrieNode {
        TrieNode[] children = new TrieNode[26];
        int idx = Integer.MAX_VALUE;
        int minLen = Integer.MAX_VALUE;
    }
    public int[] stringIndice...
 
 
Back to Top