当前位置:网站首页>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 .
边栏推荐
- What is an excellent fast development framework like?
- Methods of using arrays as function parameters in shell
- LeetCode 438. 找到字符串中所有字母异位词
- Binary to decimal, decimal to binary
- Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
- Arbre DP acwing 285. Un bal sans patron.
- LeetCode 715. Range 模块
- Dom4j遍历和更新XML
- DOM render mount patch responsive system
- file_ put_ contents
猜你喜欢

LeetCode 1089. Duplicate zero

LeetCode 1089. 复写零

What is an excellent fast development framework like?

LeetCode 57. 插入区间

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

Life cycle of Servlet

Allocation exception Servlet

Mortgage Calculator

LeetCode 532. K-diff number pairs in array

传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
随机推荐
C language file reading and writing
Try to reprint an article about CSDN reprint
Phpstudy 80 port occupied W10 system
Summary of methods for counting the number of file lines in shell scripts
Dom4j traverses and updates XML
createjs easeljs
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
Really explain the five data structures of redis
Apache startup failed phpstudy Apache startup failed
浅谈企业信息化建设
树形DP AcWing 285. 没有上司的舞会
cres
剑指 Offer II 029. 排序的循环链表
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Six dimensional space (C language)
Annotations simplify configuration and loading at startup
Binary tree sorting (C language, int type)
AcWing 787. 归并排序(模板)
我們有個共同的名字,XX工
[concurrent programming] concurrent security