当前位置:网站首页>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;
}
}果然要快很多。
边栏推荐
- createjs easeljs
- Unity editor expansion - draw lines
- The method for win10 system to enter the control panel is as follows:
- 【Rust 笔记】07-结构体
- 记忆化搜索 AcWing 901. 滑雪
- Concurrent programming (VI) ABA problems and solutions under CAS
- Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
- Complex character + number pyramid
- TP5 order multi condition sort
- PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
猜你喜欢

20220630 learning clock in

Annotations simplify configuration and loading at startup
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue

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

Alibaba canaladmin deployment and canal cluster Ha Construction

Unity editor expansion - draw lines

Dom4j traverses and updates XML
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

Markdown learning

Binary tree sorting (C language, int type)
随机推荐
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
too many open files解决方案
First Servlet
<, < <,>, > > Introduction in shell
[rust notes] 05 error handling
Monotonic stack -42 Connect rainwater
Dom4j traverses and updates XML
LeetCode 241. 为运算表达式设计优先级
【Rust 笔记】13-迭代器(上)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
Unity notes 1
Phpstudy 80 port occupied W10 system
Baidu editor ueeditor changes style
Deeply understand the underlying data structure of MySQL index
Collection interface
Apache startup failed phpstudy Apache startup failed
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
[rust notes] 11 practical features
php public private protected