LeetCode刷题笔记 面试题 17.13. 恢复空格

题目描述

哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"I reset the computer. It still didn’t boot!“已经变成了"iresetthecomputeritstilldidntboot”。在处理标点符号和大小写之前,你得先把它断成词语。当然了,你有一本厚厚的词典dictionary,不过,有些词没在词典里。假设文章用sentence表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。

示例:
输入:
dictionary = [“looked”,“just”,“like”,“her”,“brother”]
sentence = “jesslookedjustliketimherbrother”
输出: 7
解释: 断句后为"jess looked just like tim her brother",共7个未识别字符。

Sample Code

字典树

class Solution {
    class TrieNode {
        TrieNode[] childs;
        boolean isEnd;
        public TrieNode() {
            childs = new TrieNode[26];
            isEnd = false;
        }
    }
    
    TrieNode root = new TrieNode();
    public int respace(String[] dictionary, String sentence) {
        int N = sentence.length();
        helper(dictionary);
        int[] dp = new int[N+1];
        for(int i = N-1; i >= 0; i--) {
            dp[i] = N-i;
            TrieNode node = root;
            for(int j = i; j < N; j++) {
                int n = sentence.charAt(j) - 'a';
                if(node.childs[n] == null) {
                    dp[i] = Math.min(dp[i], j-i+1+dp[j+1]);
                    break;
                }
                if(node.childs[n].isEnd) 
                    dp[i] = Math.min(dp[i], dp[j+1]);
                else
                    dp[i] = Math.min(dp[i], j-i+1+dp[j+1]);
                node = node.childs[n];
            }
        }
        return dp[0];
    }
    
    private void helper(String[] dictionary) {
        for(String str: dictionary) {
            TrieNode node = root;
            for(int i = 0; i < str.length(); i++) {
                int n = str.charAt(i) - 'a';
                if(node.childs[n] == null) {
                    node.childs[n] = new TrieNode();
                }
                node = node.childs[n];        
            }
            node.isEnd = true;
        }
    }
}

Sample Code 2

暴力优化

class Solution {
    public int respace(String[] dictionary, String sentence){
        Set<String> dic = new HashSet<>();
        // <最后一个字符,这样的单词长度有哪些>
        Map<Character, Set<Integer>> map = new HashMap<>();
        for(String str: dictionary){
            dic.add(str);
            int len = str.length();
            char c = str.charAt(len-1);
            Set<Integer> set = map.getOrDefault(c, new HashSet<>());
            set.add(len);
            map.put(c, set);
        }
        
        int n = sentence.length();
        int[] dp = new int[n+1];
        for(int i=1; i<=n; i++){
            dp[i] = dp[i-1]+1;
            char c = sentence.charAt(i-1);
            if(map.containsKey(c)){
                Set<Integer> lens = map.get(c);
                Iterator<Integer> it = lens.iterator();
                while(it.hasNext()){
                    int l = it.next();
                    if(i>=l && dic.contains(sentence.substring(i-l,i))){
                        dp[i] = Math.min(dp[i], dp[i-l]);
                    }
                }
            }
        }
        return dp[n];
    }
}

Sample Code 3

暴力

class Solution {
    public int respace(String[] dictionary, String sentence) {
        Set<String> dic = new HashSet<>();
        for(String str: dictionary) dic.add(str);

        int n = sentence.length();
        //dp[i]表示sentence前i个字符所得结果
        int[] dp = new int[n+1];
        for(int i=1; i<=n; i++){
            dp[i] = dp[i-1]+1;  //先假设当前字符作为单词不在字典中
            for(int j=0; j<i; j++){
                if(dic.contains(sentence.substring(j,i))){
                    dp[i] = Math.min(dp[i], dp[j]);
                }
            }
        }
        return dp[n];
    }
}

作者:tian-ye
链接:https://leetcode-cn.com/problems/re-space-lcci/solution/cong-bao-li-ru-shou-you-hua-yi-ji-triezi-dian-shu-/
来源:力扣(LeetCode)
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

你可能感兴趣的:(LeetCode笔记,#,动态规划)