【问题描述】[中等]

【解答思路】

1. 动态规划

动态规划流程
第 1 步:设计状态
dp[i] 表示字符串的前 i 个字符的最少未匹配数。

第 2 步:状态转移方程
假设当前我们已经考虑完了前 i -1个字符了,对于前 i 个字符对应的最少未匹配数:

第 i 个字符未匹配,则 dp[i] = dp[i+1] + 1,即不匹配数加 1;
遍历前 i -1个字符,若以其中某一个下标 j 为开头、以第 i 个字符为结尾的字符串正好在词典里,则 dp[i] = min(dp[ i ], dp[ j ]) 更新 dp[i]。

第 3 步:考虑初始化
int[] dp = new int[n+1];
dp[0] = 0;
第 4 步:考虑输出
dp[n];

时间复杂度:O(N^3) 空间复杂度:O(N)

class Solution {public int respace(String[] dictionary, String sentence) {Set<String> dic = new HashSet<>();for(String str: dictionary) dic.add(str);int n = sentence.length();//dp[i]表示sentence前i个字符所得结果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];}
}
2. Trie字典树优化



复杂度分析

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;}
}
3. 字符串哈希


复杂度分析

class Solution {static final long P = Integer.MAX_VALUE;static final long BASE = 41;public int respace(String[] dictionary, String sentence) {Set<Long> hashValues = new HashSet<Long>();for (String word : dictionary) {hashValues.add(getHash(word));}int[] f = new int[sentence.length() + 1];Arrays.fill(f, sentence.length());f[0] = 0;for (int i = 1; i <= sentence.length(); ++i) {f[i] = f[i - 1] + 1;long hashValue = 0;for (int j = i; j >= 1; --j) {int t = sentence.charAt(j - 1) - 'a' + 1;hashValue = (hashValue * BASE + t) % P;if (hashValues.contains(hashValue)) {f[i] = Math.min(f[i], f[j - 1]);}}}return f[sentence.length()];}public long getHash(String s) {long hashValue = 0;for (int i = s.length() - 1; i >= 0; --i) {hashValue = (hashValue * BASE + s.charAt(i) - 'a' + 1) % P;}return hashValue;}
}

【总结】

1.动态规划流程

第 1 步:设计状态
第 2 步:状态转移方程
第 3 步:考虑初始化
第 4 步:考虑输出
第 5 步:考虑是否可以状态压缩

2. Rabin-Karp 字符串编码 (字符串哈希)

3.Trie
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;}
}

转载链接:https://leetcode-cn.com/problems/re-space-lcci/solution/hui-fu-kong-ge-by-leetcode-solution/
Rabin-Karp 字符串编码 参考链接:https://leetcode-cn.com/problems/longest-happy-prefix/solution/zui-chang-kuai-le-qian-zhui-by-leetcode-solution/

[Leetcode][程序员面试金典][面试题17.13][JAVA][恢复空格][动态规划][Trie][字符串哈希]相关推荐

  1. [Leetcode][程序员面试金典][面试题16.11][JAVA][跳水板][数学][动态规划]

    [问题描述][简单] [解答思路] 边界问题 k=0 ,不能产生跳水板,返回空数组 shorter 等于longer,只有一种跳水板,返回longerk 思路 一般情况,k块木板,k种可能 跳水板的长 ...

  2. 程序员面试金典 - 面试题 17.13. 恢复空格(DP+Trie树)

    文章目录 1. 题目 2. 解题 2.1 动态规划 2.2 Trie树 1. 题目 哦,不!你不小心把一个长篇文章中的空格.标点都删掉了,并且大写也弄成了小写. 像句子"I reset th ...

  3. [Leetcode][程序员面试金典][面试题08.03][JAVA][魔术索引][递归][优化]

    [问题描述][简单] [解答思路] 1. 逐个查找 时间复杂度:O(N) 空间复杂度:O(1) public int findMagicIndex(int[] nums) {for (int i = ...

  4. 程序员面试金典 - 面试题 17.08. 马戏团人塔(最长上升子序 DP/二分查找)

    文章目录 1. 题目 2. 解题 2.1 超时解 2.2 二分查找 1. 题目 有个马戏团正在设计叠罗汉的表演节目,一个人要站在另一人的肩膀上.出于实际和美观的考虑,在上面的人要比下面的人矮一点且轻一 ...

  5. 程序员面试金典 - 面试题 08.13. 堆箱子(DP)

    1. 题目 堆箱子.给你一堆n个箱子,箱子宽 wi.深 di.高 hi. 箱子不能翻转,将箱子堆起来时,下面箱子的宽度.高度和深度必须大于上面的箱子. 实现一种方法,搭出最高的一堆箱子.箱堆的高度为每 ...

  6. 程序员面试金典 - 面试题 17.07. 婴儿名字

    题目难度: 中等 原题链接 今天继续更新程序员面试金典系列, 大家在公众号 算法精选 里回复 面试金典 就能看到该系列当前连载的所有文章了, 记得关注哦~ 题目描述 每年,政府都会公布一万个最常见的婴 ...

  7. 程序员面试金典 - 面试题 17.06. 2出现的次数(找递推规律)

    1. 题目 编写一个方法,计算从 0 到 n (含 n) 中数字 2 出现的次数. 示例: 输入: 25 输出: 9 解释: (2, 12, 20, 21, 22, 23, 24, 25)(注意 22 ...

  8. 程序员面试金典 - 面试题 17.22. 单词转换(BFS)

    1. 题目 给定字典中的两个词,长度相等. 写一个方法,把一个词转换成另一个词, 但是一次只能改变一个字符. 每一步得到的新词都必须能在字典中找到. 编写一个程序,返回一个可能的转换序列.如有多个可能 ...

  9. 程序员面试金典 - 面试题 17.15. 最长单词(排序+递归)

    1. 题目 给定一组单词words,编写一个程序,找出其中的最长单词,且该单词由这组单词中的其他单词组合而成. 若有多个长度相同的结果,返回其中字典序最小的一项,若没有符合要求的单词则返回空字符串. ...

最新文章

  1. 实战:RocketMQ削峰,这一篇就够了
  2. win7系统怎么查看服务器IP地址,win7系统的ip地址在哪 小编教你怎么查看
  3. 卡写入速度_看清商家买相机送SD卡的套路,一文教你掌握存储卡选购秘诀
  4. mysql解压缩 1067_windows安装mysql8.0.0解压版附出现1067错误解决方法
  5. 如何使用log4j记录日志
  6. AE学习笔记——第一章:AE的界面布局和基本操作
  7. Dubbo搭建监控中心
  8. “三峡水怪”的真面目竟是这个!水怪:我不要面子的吗?
  9. 《ANTLR 4权威指南 》一导读
  10. 网页英文字体和中文字体应用
  11. mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )...
  12. Ozone SCM HA设计浅谈
  13. CI框架 where 跟 OR 怎么连用
  14. 动手学数据分析之数据加载及探索性数据分析
  15. 《京东话费充值系统架构演进实践》--阅读
  16. allegro画两层板板步骤
  17. HBuilder表单提交php出现内部服务器错误
  18. FDTD solutions——TFSF(全场散射场)光源及斜入射
  19. 黑客丛林之旅 第十关
  20. 二叉树的先序遍历(源代码)

热门文章

  1. (模板)网页游戏用的“内容区”的“图赏影音”模板
  2. org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for...
  3. JDE Client开发端 左侧边栏设置
  4. ISA Server 2004 0x80004005错误
  5. mysql inode_Linux中inode的大小、作用讲述
  6. springcloud gateway 源码解析、请求响应流程、第三方响应结果在 gateway 的经过
  7. Java学习资料汇总(JavaSE+JavaEE+数据库+框架+笔试题+面试题)
  8. android RecyclerView EditText 取消自动聚焦
  9. android 重叠view 重叠布局,按比例布局
  10. php 类学习,php的类学习(一)