当前位置:网站首页>LeetCode 30. Concatenate substrings of all words
LeetCode 30. Concatenate substrings of all words
2022-07-03 09:02:00 【Sasakihaise_】
30. Concatenate the substrings of all words
【 violence + Hashtable 】 For each character , The length of the whole string can be formed from the beginning to the end , Each move words[i] The position of the length determines whether it is in the hash table .
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;
}
}
【 The sliding window + Hash 】 We found that when a string does not match, we can jump to the position of the next string first , In this way, only the previous character is removed from the hash table .
class Solution {
// The sliding window + Hash
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;
}
}
Sure enough, it's much faster .
边栏推荐
- 干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
- LeetCode 715. Range 模块
- Phpstudy 80 port occupied W10 system
- Find the combination number acwing 886 Find the combination number II
- Really explain the five data structures of redis
- 教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
- DOM 渲染系统(render mount patch)响应式系统
- Introduction to the usage of getopts in shell
- AcWing 785. 快速排序(模板)
- Concurrent programming (III) detailed explanation of synchronized keyword
猜你喜欢
随机推荐
Basic knowledge of network security
高斯消元 AcWing 883. 高斯消元解线性方程组
Vscode connect to remote server
Noip 2002 popularity group selection number
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
我们有个共同的名字,XX工
What is the difference between sudo apt install and sudo apt -get install?
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
Solution of 300ms delay of mobile phone
LeetCode 1089. 复写零
Monotonic stack -42 Connect rainwater
Arbre DP acwing 285. Un bal sans patron.
[concurrent programming] explicit lock and AQS
[rust note] 10 operator overloading
网络安全必会的基础知识
剑指 Offer II 091. 粉刷房子
LeetCode 535. TinyURL 的加密与解密
MySQL three logs
AcWing 787. 归并排序(模板)
[rust notes] 08 enumeration and mode