当前位置:网站首页>Hot100 单词拆分
Hot100 单词拆分
2022-07-21 12:15:00 【爪哇贡尘拾Miraitow】
给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s 。
注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
HashSet<String> hashSet = new HashSet<String>();
for(String t : wordDict) hashSet.add(t);
boolean[] f = new boolean[n + 1];
f[0] = true;
for(int i = 1; i <= n; i++){
for(int j = 0; j < i; j++){
String t = s.substring(j, i);
if(f[j] == true && hashSet.contains(t)){
f[i] = true;
break; //有一个方案合法就可以结束了
}
}
}
return f[n];
}
}
作者:lin-shen-shi-jian-lu-k
链接:https://leetcode.cn/problems/word-break/solution/by-lin-shen-shi-jian-lu-k-yl5g/
来源:力扣(LeetCode)
边栏推荐
猜你喜欢
上海交大团队使用联合深度学习优化代谢组学研究
Medical cell image segmentation
Interview must ask: from entering URL to page display, what happened? (detailed and easy to understand, organized and easy to remember)
Latex combines multiple subgraphs
The MathType DLL cannot be found.Please reinstall Math问题已解决
(resolved) for camerads, HR = m_ pGraph-&gt; Connect(m_pCameraOutput, m_pGrabberInput); Solutions to implementation failures
MapStruct代码生成器实现对象转换
Musk claims to be on the brain. Is it science or deception?
ionic4学习笔记8--UI组件2列表(没有实践,直接摘抄)
Download tools - Google plug-ins tempermonkey and greasyfork
随机推荐
Solution to unknown host problem
Fake death occurs when Google browser is saved as an image
highcharts图表及报表展示、导出数据
鼠标点击网页任意地方都是光标闪烁输入状态
单利模式的四种方式
js如何控制整个页面滚动条的位置
4.基本类型和引用类型?
Go语言 接口与类型
7.字符编码?
上海交大团队使用联合深度学习优化代谢组学研究
margin的塌陷问题
快速判断一个文件是否有病毒
写页面注意的问题
Web server
C语言中不同类型算术运算隐式转换规则验证
风控文章资源合集
. Replacewith() can only work once
ionic4学习笔记12-某东项目栅格完成商品列表
Collection of risk control article resources
登陆状态如何管理?登录流程?