首页 > 其他 > 详细

包含所有前缀的最长字符串

时间:2021-09-02 21:13:42      阅读:39      评论:0      收藏:0      [点我收藏+]

 

可以有两种方式来实现:

前缀树

HashMap

 public static void main(String[] args) {
        String[] listStr={"k","ab","a","kie","kit","kitt","kittty"};
        HashMap<String,Integer> map=new HashMap<>();
        for(String str:listStr){
            map.put(str,1);
        }
        PriorityQueue<String> priorityQueue=new PriorityQueue<>((a,b)->b.length()-a.length());
        for(String str:listStr){
            int endSize=0;
            for(int i=1;i<=str.length();i++){
                String curStr=str.substring(0,i);
                if(map.containsKey(curStr)){
                    endSize++;
                }
            }
            if(endSize==str.length()){
                priorityQueue.add(str);
            }
        }
        System.out.println(priorityQueue.peek());
    }

  

包含所有前缀的最长字符串

原文:https://www.cnblogs.com/iwyc/p/15215977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!