LeetCode 642: Design Search Autocomplete System

Note:

MANY DETAILS ARE MISSED:

1. If less than 3, return as many as it. So it could be NPE when queue.poll().getKey()

2. Use alphabetical order if times are same. So in the queu need to compare value first then compare string.

3. Logically it needs to set children first and move to child to add counts.

 

 

class AutocompleteSystem {
    class TrieNode {
        public Map<Character, TrieNode> children;
        public Map<String, Integer> counts;
        public TrieNode() {
            children = new HashMap<>();
            counts = new HashMap<>();
        }
    }
    
    private TrieNode _root;
    private StringBuilder _prefix;

    public AutocompleteSystem(String[] sentences, int[] times) {
        _root = new TrieNode();
        _prefix = new StringBuilder();
        for (int i = 0; i < sentences.length; i++) {
            addSentence(sentences[i], times[i]);
        }
    }
    
    private void addSentence(String sentence, int times) {
        TrieNode current = _root;
        for (char c : sentence.toCharArray()) {
            if (!current.children.containsKey(c)) {
                current.children.put(c, new TrieNode());
            }
            current = current.children.get(c);
            current.counts.put(sentence, current.counts.getOrDefault(sentence, 0) + times);
        }
    } 
    
    public List<String> input(char c) {
        if (c == '#') {
            addSentence(_prefix.toString(), 1);
            _prefix.setLength(0);
            return Collections.emptyList();
        }
        _prefix.append(c);
        TrieNode current = _root;
        for (char runner : _prefix.toString().toCharArray()) {
            if (!current.children.containsKey(runner)) {
                return Collections.emptyList();
            }
            current = current.children.get(runner);
        }
        
        PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>((a, b) -> a.getValue().compareTo(b.getValue()) == 0 ? a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue());
        queue.addAll(current.counts.entrySet());
        
        List<String> result = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Map.Entry<String, Integer> entry = queue.poll();
            if (entry != null) {
                result.add(entry.getKey());
            }
        }
        return result;
    }
}

/**
 * Your AutocompleteSystem object will be instantiated and called as such:
 * AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
 * List<String> param_1 = obj.input(c);
 */

 

转载于:https://www.cnblogs.com/shuashuashua/p/7490293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值