LeetCode每日一题--面试题16.18. 模式匹配(Java)

面试题 17.13. 恢复空格

20200709

难度:中等

题目描述

哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"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个未识别字符。

提示:

  • 0 <= len(sentence) <= 1000
  • dictionary中总字符数不超过 150000。
  • 你可以认为dictionary和sentence中只包含小写字母。

Solution

  1. 暴力动态规划
  • 状态:dp[i]代表前i个字符中的最少未识别的字符数
  • 状态转移
    • 如果i前面的单词sentence[j−1⋯i−1]在字典中,则dp[i]=min(dp[i],dp[j])
    • 否则,dp[i]=dp[i−1]+1
class Solution {
    public int respace(String[] dictionary, String sentence) {
        Set<String> dic = new HashSet<>();
        for(String s : dictionary){
            dic.add(s);
        }
        int n = sentence.length();
        // dp[i]表示[0,i-1]的最小的未识别的字符数
        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];
    }
}
  1. 改成遍历字典,比上面那种要快一点
class Solution {
    public int respace(String[] dictionary, String sentence) {
        Set<String> dic = new HashSet<>();
        for(String s : dictionary){
            dic.add(s);
        }
        int n = sentence.length();
        // dp[i]表示[0,i-1]的最小的未识别的字符数
        int[] dp = new int[n+1];
        for(int i = 1; i <= n; i++){
            //先假设当前字符作为单词不在字典中
            dp[i] = dp[i-1] + 1;
             // 遍历字典,如匹配上则更新dp数组
            for(int j = 0; j < dictionary.length; j++){
                int len = dictionary[j].length();
                if(len > i){
                    continue;
                }
                if(dictionary[j].equals(sentence.substring(i-len, i))){
                    dp[i] = Math.min(dp[i], dp[i-len]);
                }
            }
        }
        return dp[n];
    }
}
  1. Trie字典树优化

    字典树可利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较

    a) dp倒着来

class Solution {
    public int respace(String[] di, String se) {
        int n = se.length();
        Trie root = new Trie();

        for(String s : di){
            root.insert(s);
        }
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for(int i = 1; i <= n; i++){
            dp[i] = dp[i - 1] + 1;
            Trie cur = root;
            for(int j = i; j >= 1; j--){
                int m = se.charAt(j - 1) - 'a';
                if(cur.next[m] == null) break;
                else if(cur.next[m].isEnd) dp[i] = Math.min(dp[i], dp[j - 1]);

                if(dp[i] == 0) break;
                cur = cur.next[m];
            }
        }
        return dp[n];
    }
}

class Trie{
    public Trie[] next;
    public boolean isEnd;

    public Trie(){
        this.next = new Trie[26];
        this.isEnd = false;
    }
    public void insert(String s){
        Trie cur = this;

        for(int i = s.length() - 1; i >= 0; i--){
            int m = s.charAt(i) - 'a';
            if(cur.next[m] == null){
                cur.next[m] = new Trie();
            }
            cur = cur.next[m];
        }
        cur.isEnd = true;
    }

}

GitKid

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值