题解如下:

/**
* 动态规划解法:
* dp[i] 表示 0-i的最小不能被识别的字母个数
* 求 dp[k] 如果第K个字母 不能和前面的字母[0-{k-1}]合在一起被识别 那么dp[k] = dp[k-1]+1
* 如果可以别识别 dp[k] = min(dp[k],dp[j-1])
*
* 能不能被识别的判断又有几种解法:
* 1. 使用HashMap -- 本题解使用HashMap
* 2. 使用字典树
* 3. RK算法
* 参考链接:https://leetcode-cn.com/problems/re-space-lcci/solution/hui-fu-kong-ge-by-leetcode-solution/
* @param dictionary
* @param sentence
* @return
*/
public int respace3(String[] dictionary, String sentence) { HashMap<String, Integer> map = new HashMap<>();
for (String s : dictionary) {
map.put(s,1);
}
int length = sentence.length();
int[] dp = new int[length+1];
// 由于边界的处理我们让dp[i+1] 表示0-i的最小不能被识别的字母个数
for (int i = 0; i < length; i++) {
dp[i+1] = dp[i]+1;
for (int j = 0; j <= i; j++) {
if (map.getOrDefault(sentence.substring(j,i+1),0) == 1) {
dp[i+1] = Math.min(dp[i+1],dp[j]);
}
}
}
return dp[length]; }

Leetcode----<Re-Space LCCI>的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. Java 图像处理(一)

    曾几何时,Java图像处理已经被认为是太过鸡肋,就连Java的创始公司,在java图像处理方面也是浅尝辄止,可能相比较C++,Java在这方面的处理,确实差强人意. 不过Java类库中有一个叫JAI的 ...

  2. javaWeb代码整理01-mysql

    jar包: maven坐标: <dependency> <groupId>mysql</groupId> <artifactId>mysql-conne ...

  3. 2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串)

    2021.12.09 [HEOI2016/TJOI2016]排序(线段树+二分,把一个序列转换为01串) https://www.luogu.com.cn/problem/P2824 题意: 在 20 ...

  4. docker进阶_dockerswarm

    DockerSwarm Docker Swarm简介 Docker Swarm的功能 ​ Docker Swarm包含两个方面:docker安全集群,以及一个微服务应用引擎 ​ 集群方面,swarm将 ...

  5. python基础练习题(题目 统计 1 到 100 之和)

    day31 --------------------------------------------------------------- 实例045:求和 题目 统计 1 到 100 之和. 分析: ...

  6. python 多进程共享全局变量之Manager()

    Manager支持的类型有list,dict,Namespace,Lock,RLock,Semaphore,BoundedSemaphore,Condition,Event,Queue,Value和A ...

  7. Microsoft Graph 的 .NET 6 之旅

    这是一篇发布在dotnet 团队博客上由微软Graph首席软件工程师 Joao Paiva写的文章,原文地址: https://devblogs.microsoft.com/dotnet/micros ...

  8. AspNetCore开源中间件-VueRouterHistory

    前言 用过VueRouter路由组件的应该都知道,VueRouter有hash和history两种模式.hash模式会在url中插入#,history模式下url则看上去更加简洁美观.如果想要支持hi ...

  9. 低代码 —— 初步认识 Appsmith

    初步认识 Appsmith appsmith 是什么 appsmith 是 github 上的一个开源项目,截至此刻(20220512)有 17.7k Star. Appsmith 是一个低代码.开源 ...

  10. DirectX11 With Windows SDK--39 阴影技术(VSM、ESM)

    前言 上一章我们介绍了级联阴影贴图.刚开始的时候我尝试了给CSM直接加上PCSS,但不管怎么调难以达到说得过去的效果.然后文章越翻越觉得阴影就是一个巨大的坑,考虑到时间关系,本章只实现了方差阴影贴图( ...