首页 > 其他分享 >【leetcode】【100268. 最长公共后缀查询】【Trie树】

【leetcode】【100268. 最长公共后缀查询】【Trie树】

时间:2024-03-24 16:24:48浏览次数:25  
标签:Node index Trie 100268 int length new wordsContainer leetcode

How to slove this problem?

  1. If we reverse the strings, the problem changes to finding the longest common prefix.
  2. Build a Trie, each node is a letter and only saves the best word’s index in each node, based on the criteria
  3. .code:
    class Solution {
        public Node ROOT;
        public String[] wordsContainer;
        public static final char INVALID = '1';
        public int minIndex = 1000_000_000;
        public int[] stringIndices(String[] wordsContainer, String[] wordsQuery) {
            this.wordsContainer = new String[wordsContainer.length];
            minIndex = 1000_000_000;
            int minlen = 1000_000_000;
            for (int i = 0; i < wordsContainer.length; i++) {
                this.wordsContainer[i] = new StringBuilder(wordsContainer[i]).reverse().toString();
                if (minlen > wordsContainer[i].length()) {
                    minlen = wordsContainer[i].length();
                    minIndex = i;
                }
            }
    
            ROOT = new Node(INVALID, minIndex);
    
            for (int i = 0; i < this.wordsContainer.length; i++) {
                buildTree(this.wordsContainer[i], i);
            }
            int[] ans = new int[wordsQuery.length];
            for (int i = 0; i < wordsQuery.length; i++) {
                String query = new StringBuilder(wordsQuery[i]).reverse().toString();
                ans[i] = getIndex(query);
            }
            return ans;
        }
    
        public int getIndex(String query) {
            int treeId = query.charAt(0) - 'a';
            Node tree = ROOT.children[treeId];
            if (tree == null) {
                return minIndex;
            }
            int ans = tree.index;
            Node curr = tree;
            for (int j = 1; j < query.length(); j++) {
                Node next = curr.children[query.charAt(j)-'a'];
                if( next != null){
                    ans = next.index;
                }else{
                    return ans;
                }
                curr = next;
            }
            return ans;
        }
    
    
        public void buildTree(String str, int index) {
            int treeId = str.charAt(0) - 'a';
            Node[] trees = ROOT.children;
    
            if (trees[treeId] == null) {
                trees[treeId] = new Node(str.charAt(0), index);
            } else {
                int origin = trees[treeId].index;
                if (wordsContainer[origin].length() > str.length()) {
                    trees[treeId].index = index;
                }
            }
    
            Node root = trees[treeId];
            Node curr = root;
    
            for (int j = 1; j < str.length(); j++) {
                Node existNode = curr.children[str.charAt(j) - 'a'];
                if (existNode != null) {
                    existNode.index = wordsContainer[existNode.index].length() > str.length() ? index : existNode.index;
                } else {
                    existNode = new Node(str.charAt(j), index, curr);
                }
                curr.children[str.charAt(j) - 'a'] = existNode;
                curr = existNode;
            }
        }
    
        class Node {
            char letter;
            int index;
            Node parent;
            Node[] children;
            public Node(char letter, int index) {
                this.letter = letter;
                this.index = index;
                parent = null;
                children = new Node[26];
            }
    
            public Node(char letter, int index, Node parent) {
                this.letter = letter;
                this.index = index;
                this.parent = parent;
                children = new Node[26];
            }
        }
    }
 

标签:Node,index,Trie,100268,int,length,new,wordsContainer,leetcode
From: https://www.cnblogs.com/fishcanfly/p/18092577

相关文章

  • LeetCodeHot100 栈 155. 最小栈 394. 字符串解码
    155.最小栈https://leetcode.cn/problems/min-stack/description/?envType=study-plan-v2&envId=top-100-likedclassMinStack{Deque<Integer>deque;PriorityQueue<Integer>priorityQueue;publicMinStack(){de......
  • java数据结构与算法刷题-----LeetCode75. 颜色分类
    java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846文章目录1.双指针两次遍历2.三指针1.双指针两次遍历解题思路:时间复杂度O(......
  • java数据结构与算法刷题-----LeetCode451. 根据字符出现频率排序
    java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846文章目录1.hash统计出现次数后排序2.桶排序1.hash统计出现次数后排序解题思路:时间复杂度O(......
  • LeetCode 1778. Shortest Path in a Hidden Grid
    原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-hidden-grid/description/题目:Thisisan interactiveproblem.Thereisarobotinahiddengrid,andyouaretryingtogetitfromitsstartingcelltothetargetcellinthisgrid.Thegridiso......
  • LeetCode 834. Sum of Distances in Tree
    原题链接在这里:https://leetcode.com/problems/sum-of-distances-in-tree/description/题目:Thereisanundirectedconnectedtreewith n nodeslabeledfrom 0 to n-1 and n-1 edges.Youaregiventheinteger n andthearray edges where edges[i]=[a......
  • 和为 K 的子数组 - LeetCode 热题 10
    大家好!我是曾续缘......
  • 找到字符串中所有字母异位词 - LeetCode 热题 9
    大家好!我是曾续缘......
  • leetcode684冗余连接(模板题,理解背过就行)
    leetcode684冗余连接(模板题,理解背过就行)考了图的连通,笔试碰见了还好老底没忘,不然就尬住了,总结一下。一、题目树可以看成是一个连通且无环的无向图。给定往一棵n个节点(节点值1~n)的树中添加一条边后的图。添加的边的两个顶点包含在1到n中间,且这条附加的边不属......
  • LeetCode刷题记录——day5
    1、https://leetcode.cn/problems/roman-to-integer/solutions/1/bao-li-po-jie-by-a-studentdog-s1va/?envType=study-plan-v2&envId=top-interview-150关键在于创建字典classSolution{public:intromanToInt(strings){unordered_map<string,int>m=......
  • 【LeetCode 552】学生出勤记录II
    题目描述原题链接:LeetCode.552学生出勤记录II解题思路根据题意,缺勤天数最多只有一天,迟到最多只能连续两天,可以按末尾两个字符状态作为DP数组含义的不同维度往后递推长度增长时的数量值。dp[i][j]中的i表示长度为i的出勤记录,j表示末尾字符状态:j的值含义0无......