当前位置:网站首页>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;
}
}果然要快很多。
边栏推荐
- [concurrent programming] collaboration between threads
- Using DLV to analyze the high CPU consumption of golang process
- php public private protected
- TP5 order multi condition sort
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- 我们有个共同的名字,XX工
- Allocation exception Servlet
- Message queue for interprocess communication
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- 求组合数 AcWing 886. 求组合数 II
猜你喜欢

Unity editor expansion - controls, layouts

I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made

Really explain the five data structures of redis

数位统计DP AcWing 338. 计数问题

Debug debugging - Visual Studio 2022

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

树形DP AcWing 285. 没有上司的舞会
![[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](/img/76/6561a78b7f883a0e75a53e037153c3.jpg)
[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
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

Message queue for interprocess communication
随机推荐
Unity editor expansion - controls, layouts
Summary of methods for counting the number of file lines in shell scripts
Deep parsing JVM memory model
Get the link behind? Parameter value after question mark
Monotonic stack -42 Connect rainwater
<, < <,>, > > Introduction in shell
求组合数 AcWing 885. 求组合数 I
Method of intercepting string in shell
Unity Editor Extension - event handling
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
[rust notes] 07 structure
Dom4j遍历和更新XML
Try to reprint an article about CSDN reprint
Development experience and experience
[concurrent programming] Table hopping and blocking queue
如何应对数仓资源不足导致的核心任务延迟
[rust notes] 05 error handling
Apache startup failed phpstudy Apache startup failed
MySQL three logs
Final review of Database Principles