每日一题(2020-07-09)面试题 17.13. 恢复空格

面试题 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个未识别字符。

提示:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/re-space-lcci

解法一:动态规划

dp[i] 表示字符串的前 i 个字符的最少未匹配数。

假设当前我们已经考虑完了前 i 个字符了,对于前 i + 1 个字符对应的最少未匹配数:

  • 如果第 i + 1 个字符未匹配,则 dp[i + 1] = dp[i] + 1 ;
  • 遍历前 i 个字符,若以其中某一个下标 index为开头、以第 i + 1 个字符为结尾的字符串正好在词典里,则 dp[i] = min(dp[i], dp[index])。
class Solution {
    public int respace(String[] dictionary, String sentence) {
        Set<String> dict = new HashSet<>();
        //将字典 dictionary 中的单词加入哈希表中
        for(String s : dictionary) {
        	dict.add(s);
        }
        int len = sentence.length();
        int[] dp = new int[len + 1];
        for (int i = 1; i <= len; i++) {
        	//先假设为最坏的情况
            dp[i] = dp[i - 1] + 1;
            //枚举以  sentence[i - 1] 为尾的子串
            for (int index = 0; index < i; index++) {
                if (dict.contains(sentence.substring(index, i))) {
                    dp[i] = Math.min(dp[i], dp[index]);
                }
            }
        }
        return dp[len];
    }
}

解法二:动态规划+字典树

解法一在枚举以 sentence[i - 1] 为尾的子串的过程中,存在着很多无用操作。例如 如果不存在以 sentence[i - 1] 结尾的子串时,可以直接跳过枚举 [0, i -2] 范围开头的子串。

class Solution {
    public int respace(String[] dictionary, String sentence) {
        // 构建字典树
        Trie trie = new Trie();
        for (String word: dictionary) {
            trie.insert(word);
        }
        int len = sentence.length();
        int[] dp = new int[len + 1];
        for (int i = 1; i <= len; i++) {
            dp[i] = dp[i - 1] + 1;
            for (int index: trie.search(sentence, i - 1)) {
                dp[i] = Math.min(dp[i], dp[index]);
            }
        }
        return dp[len];
    }
}

class Trie {
    TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    // 将单词倒序插入字典树
    public void insert(String word) {
        TrieNode current = root;
        for (int i = word.length() - 1; i >= 0; i--) {
            int index = word.charAt(i) - 'a';
            if (current.children[index] == null) {
                current.children[index] = new TrieNode();
            }
            current = current.children[index];
        }
        current.isWord = true;
    }

    // 找到 sentence 中以 endPos 为结尾的单词,返回这些单词的开头下标。
    public List<Integer> search(String sentence, int endPos) {
        List<Integer> indices = new ArrayList<>(); 
        TrieNode current = root;
        for (int i = endPos; i >= 0; i--) {
            int index = sentence.charAt(i) - 'a';
            if (current.children[index] == null) {
                break;
            }
            current = current.children[index];
            if (current.isWord) {
                indices.add(i);
            }  
        }
        return indices;
    }
}

class TrieNode {
    boolean isWord;
    TrieNode[] children;

    public TrieNode() {
    	this.children = new TrieNode[26];
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两只Tigers跑得快

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值