当前位置:网站首页>LeetCode 30. 串联所有单词的子串
LeetCode 30. 串联所有单词的子串
2022-07-03 08:49:00 【Sasakihaise_】

【暴力+哈希表】对于每一个字符,都暴力遍历从他开始到一直到后面能组成整个字符串的长度,每次移动words[i]长度的位置就判断是否在哈希表中。
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
int m = words.length, n = words[0].length();
Map<String, Integer> map = new HashMap();
for(var str: words) map.put(str, map.getOrDefault(str, 0) + 1);
int l = s.length() - m * n;
List<Integer> ans = new ArrayList();
for(var i = 0; i <= l; i++){
Map<String, Integer> tab = new HashMap(map);
int flag = 1;
for(var j = 1; j <= m; j++){
String tmp = s.substring(i + (j - 1) * n, i + j * n);
if(tab.containsKey(tmp)) {
if(tab.get(tmp) == 1) tab.remove(tmp);
else tab.put(tmp, tab.get(tmp) - 1);
}
else{
flag = 0; break;
}
}
if(flag == 1) ans.add(i);
}
return ans;
}
}【滑动窗口+哈希】我们发现当一个字符串不匹配的时候我们可以先跳到下一个字符串的位置,这样哈希表只去掉之前那一个字符就行。
class Solution {
// 滑动窗口+哈希
public List<Integer> findSubstring(String s, String[] words) {
int m = s.length(), n = words.length, l = m - n, k = words[0].length();
Map<String, Integer> map = new HashMap();
List<Integer> ans = new ArrayList();
for(var str: words) map.put(str, map.getOrDefault(str, 0) + 1);
for(var i = 0; i < k; i++){
int j = i;
Map<String, Integer> tmp = new HashMap();
while(j + k * n <= m){
// System.out.println(j);
if(j == i){
for(var p = 0; p < n; p++){
var str = s.substring(j + p * k, j + (p + 1) * k);
tmp.put(str, tmp.getOrDefault(str, 0) + 1);
}
if(map.equals(tmp)) {
ans.add(j);
}
// tmp.forEach((x, y) -> {
// System.out.print(x + " " + y + ", ");
// });
// System.out.println();
}else{
var old = s.substring(j - k, j);
if(tmp.get(old) == 1) tmp.remove(old);
else tmp.put(old, tmp.getOrDefault(old, 0) - 1);
var neww = s.substring(j + (n - 1) * k, j + n * k);
tmp.put(neww, tmp.getOrDefault(neww, 0) + 1);
if(map.equals(tmp)) {
ans.add(j);
}
// tmp.forEach((x, y) -> {
// System.out.print(x + " " + y + ", ");
// });
// System.out.println();
}
j += k;
}
}
return ans;
}
}果然要快很多。
边栏推荐
- Log4j2 vulnerability recurrence and analysis
- Unity editor expansion - controls, layouts
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- Gif remove blank frame frame number adjustment
- DOM 渲染系统(render mount patch)响应式系统
- Unity multi open script
- 【Rust笔记】02-所有权
- [rust notes] 07 structure
- 【Rust 笔记】09-特型与泛型
- Annotations simplify configuration and loading at startup
猜你喜欢
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

数据库原理期末复习

Binary tree sorting (C language, char type)

求组合数 AcWing 886. 求组合数 II

Notes and bugs generated during the use of h:i:s and y-m-d

Markdown learning

樹形DP AcWing 285. 沒有上司的舞會

DOM render mount patch responsive system

On the setting of global variable position in C language

Debug debugging - Visual Studio 2022
随机推荐
[rust notes] 12 closure
Eating fruit
[concurrent programming] concurrent security
【Rust 笔记】11-实用特型
记忆化搜索 AcWing 901. 滑雪
Facial expression recognition based on pytorch convolution -- graduation project
[concurrent programming] collaboration between threads
单调栈-503. 下一个更大元素 II
Find the combination number acwing 886 Find the combination number II
注解简化配置与启动时加载
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Binary to decimal, decimal to binary
How to deal with the core task delay caused by insufficient data warehouse resources
22-05-26 西安 面试题(01)准备
20220630学习打卡
Find the combination number acwing 885 Find the combination number I
PHP function date (), y-m-d h:i:s in English case
Using variables in sed command
使用dlv分析golang进程cpu占用高问题
Baidu editor ueeditor changes style