1 Star 7 Fork 2

蔚蔚樱软件开发/AlgoHub

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
StringInDic.java 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
ljfirst 提交于 2022-10-31 23:58 +08:00 . feat: update
package Algorithm.dynamic;
import Top100.Dynamic;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author 蔚蔚樱
* @version 1.0
* @date 2020/6/29
* @author—Email micromicrohard@outlook.com
* @blogURL https://blog.csdn.net/Micro_Micro_Hard
* @description 字符组成判断
* 给定一个非空的字符串s和一个字典wordDict
* 判断 s 是否可以由 wordDict 里面的词组成
* s = "leetcode",
* dict = ["leet", "code"]
*/
public class StringInDic implements Dynamic {
public boolean findWord(String s, String[] wordDict) {
if (!check(s, wordDict)) {
return false;
}
//位置0是起始点,真正对比是在1号位开始,所以总体长度是s.length() + 1
boolean[] flagArray = new boolean[s.length() + 1];
//启动项,一定要置为true
flagArray[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = i - 1; j >= 0; j--) {
String temp = s.substring(j, i);
if (flagArray[j] && findWordinwordDict(temp, wordDict)) {
flagArray[i] = true;
break;//防止 被改写
}
}
}
return flagArray[s.length()];
}
public boolean findWordinwordDict(String s1, String[] wordDict) {
List l = Arrays.stream(wordDict).filter(x -> x.equals(s1)).collect(Collectors.toList());
return l.size() != 0;
/*for (String ss : wordDict) {
if (ss.equals(s1)) {
return true;
}
}
return false;*/
}
private boolean check(String s, String[] wordDict) {
//字符串不是空串,字典部位空
if (s == null || s.length() == 0 || wordDict == null || wordDict.length == 0) {
return false;
}
//stream 写法
return wordDict.length == Arrays.stream(wordDict).filter(s1 -> (s1 != null || s1.length() != 0)).count();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/micromicrohard/algo-hub.git
git@gitee.com:micromicrohard/algo-hub.git
micromicrohard
algo-hub
AlgoHub
master

搜索帮助