当前位置:网站首页>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 .
边栏推荐
- How to delete CSDN after sending a wrong blog? How to operate quickly
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Common DOS commands
- 树形DP AcWing 285. 没有上司的舞会
- 教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
- LeetCode 871. 最低加油次数
- 记忆化搜索 AcWing 901. 滑雪
- AcWing 788. 逆序对的数量
- How to use Jupiter notebook
猜你喜欢

Slice and index of array with data type

Binary to decimal, decimal to binary

Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?

Wonderful review | i/o extended 2022 activity dry goods sharing

常见渗透测试靶场

Tree DP acwing 285 A dance without a boss

Gif remove blank frame frame number adjustment
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

Log4j2 vulnerability recurrence and analysis

Facial expression recognition based on pytorch convolution -- graduation project
随机推荐
AcWing 787. 归并排序(模板)
DOM render mount patch responsive system
LeetCode 324. 摆动排序 II
Using variables in sed command
22-05-26 西安 面试题(01)准备
拯救剧荒,程序员最爱看的高分美剧TOP10
On the setting of global variable position in C language
浅谈企业信息化建设
树形DP AcWing 285. 没有上司的舞会
Monotonic stack -503 Next bigger Element II
Dom4j遍历和更新XML
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
我們有個共同的名字,XX工
[concurrent programming] concurrent security
AcWing 788. 逆序对的数量
file_ put_ contents
Monotonic stack -84 The largest rectangle in the histogram