当前位置 : 主页 > 编程语言 > java >

#yyds干货盘点# LeetCode程序员面试金典:恢复空格

来源:互联网 收集:自由互联 发布时间:2023-03-22
题目: 哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"I reset the computer. It still didn’t boot!"已经变成了"iresetthecomputeritstilldidntboot"。在处理

题目:

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

代码实现:

class Solution { public int respace(String[] dictionary, String sentence) { int n = sentence.length(); Trie root = new Trie(); for (String word: dictionary) { root.insert(word); } 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 curPos = root; for (int j = i; j >= 1; --j) { int t = sentence.charAt(j - 1) - 'a'; if (curPos.next[t] == null) { break; } else if (curPos.next[t].isEnd) { dp[i] = Math.min(dp[i], dp[j - 1]); } if (dp[i] == 0) { break; } curPos = curPos.next[t]; } } return dp[n]; }}class Trie { public Trie[] next; public boolean isEnd; public Trie() { next = new Trie[26]; isEnd = false; } public void insert(String s) { Trie curPos = this; for (int i = s.length() - 1; i >= 0; --i) { int t = s.charAt(i) - 'a'; if (curPos.next[t] == null) { curPos.next[t] = new Trie(); } curPos = curPos.next[t]; } curPos.isEnd = true; }}
上一篇:#yyds干货盘点# LeetCode面试题:螺旋矩阵
下一篇:没有了
网友评论