面试题 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。- 你可以认为
dictionary
和sentence
中只包含小写字母。
来源:力扣(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];
}
}