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中只包含小写字母。
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];
}
}
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];
}
}
Trie字典树优化
字典树可利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较
a) dp的时候从sentence最后一个字符开始
class Solution {
/** 自定义一个TrieNode类型。
* 这里不用建一个变量来存当前节点表示的字符,
* 因为只要该节点不为null,就说明存在这个字符
*/
class TrieNode{
TrieNode[] childs;
boolean isWord;
public TrieNode(){
childs = new TrieNode[26];
isWord = false;
}
}
TrieNode root; //空白的根节点,设为全局变量更醒目
public int respace(String[] dictionary, String sentence){
root = new TrieNode();
makeTrie(dictionary); //创建字典树
int n = sentence.length();
int[] dp = new int[n+1];
//这里从sentence最后一个字符开始
for(int i=n-1; i>=0; i--){
dp[i] = n-i; //初始默认后面全不匹配
TrieNode node = root;
for(int j=i; j<n; j++){
int c = sentence.charAt(j)-'a';
if(node.childs[c] == null){
//例如"abcde",i=1,j=2 可找出长度关系
dp[i] = Math.min(dp[i], j-i+1+dp[j+1]);
break;
}
if(node.childs[c].isWord){
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[c];
}
}
return dp[0];
}
public void makeTrie(String[] dictionary){
for(String str: dictionary){
TrieNode node = root;
for(int k=0; k<str.length(); k++){
int i = str.charAt(k)-'a';
if(node.childs[i] == null){
node.childs[i] = new TrieNode();
}
node = node.childs[i];
}
node.isWord = true; //单词的结尾
}
}
}
b) sentence还是从前往后遍历,字典里的单词倒着放进树
class Solution {
class TrieNode{
TrieNode[] childs;
boolean isWord;
public TrieNode(){
childs = new TrieNode[26];
isWord = false;
}
}
TrieNode root;
public int respace(String[] dictionary, String sentence){
root = new TrieNode();
makeTrie(dictionary); //创建字典树
int n = sentence.length();
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;
TrieNode node = root;
for(int j = i; j >= 1; j--){
int t = sentence.charAt(j-1) - 'a';
if(node.childs[t] == null){
break;
}else if(node.childs[t].isWord){
dp[i] = Math.min(dp[i], dp[j-1]);
}
if(dp[i] == 0){
break;
}
node = node.childs[t];
}
}
return dp[n];
}
public void makeTrie(String[] dictionary){
for(String str: dictionary){
TrieNode node = root;
for(int k = str.length() - 1; k >= 0; k--){
int t = str.charAt(k)-'a';
if(node.childs[t] == null){
node.childs[t] = new TrieNode();
}
node = node.childs[t];
}
node.isWord = true; //单词的结尾
}
}
}
欢迎关注公众号:GitKid,暂时每日分享LeetCode题解,在不断地学习中,公众号内容也在不断充实,欢迎扫码关注