当前位置:网站首页>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;
}
}
果然要快很多。
边栏推荐
- Alibaba canal actual combat
- file_ put_ contents
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
- Analysis of Alibaba canal principle
- [concurrent programming] explicit lock and AQS
- The method for win10 system to enter the control panel is as follows:
- Binary tree sorting (C language, char type)
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- [rust notes] 13 iterator (Part 1)
- Drawing maze EasyX library with recursive backtracking method
猜你喜欢
Markdown learning
Servlet的生命周期
第一个Servlet
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
状态压缩DP AcWing 91. 最短Hamilton路径
Format - C language project sub file
Tree DP acwing 285 A dance without a boss
Analysis of Alibaba canal principle
Complex character + number pyramid
20220630 learning clock in
随机推荐
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
LeetCode 241. 为运算表达式设计优先级
状态压缩DP AcWing 91. 最短Hamilton路径
[redis] redis persistent RDB vs AOF (source code)
[rust notes] 06 package and module
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
Concurrent programming (VI) ABA problems and solutions under CAS
createjs easeljs
分配异常的servlet
Using DLV to analyze the high CPU consumption of golang process
【Rust 笔记】07-结构体
Summary of methods for counting the number of file lines in shell scripts
How to deal with the core task delay caused by insufficient data warehouse resources
Unity editor expansion - scrolling list
[concurrent programming] Table hopping and blocking queue
MySQL three logs
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
Memory search acwing 901 skiing
Monotonic stack -84 The largest rectangle in the histogram