当前位置:网站首页>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 .
边栏推荐
- file_ put_ contents
- LeetCode 75. 颜色分类
- Dom4j遍历和更新XML
- Methods of checking ports according to processes and checking processes according to ports
- 我們有個共同的名字,XX工
- 使用dlv分析golang进程cpu占用高问题
- [rust notes] 12 closure
- Find the combination number acwing 885 Find the combination number I
- Convert video to GIF
- Concurrent programming (III) detailed explanation of synchronized keyword
猜你喜欢
随机推荐
求组合数 AcWing 886. 求组合数 II
MySQL three logs
使用dlv分析golang进程cpu占用高问题
[concurrent programming] collaboration between threads
记忆化搜索 AcWing 901. 滑雪
[rust notes] 13 iterator (Part 1)
LeetCode 535. TinyURL 的加密与解密
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
Find the combination number acwing 886 Find the combination number II
On the difference and connection between find and select in TP5 framework
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
URL backup 1
What is an excellent fast development framework like?
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
状态压缩DP AcWing 291. 蒙德里安的梦想
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
LeetCode 871. 最低加油次数
[rust notes] 08 enumeration and mode
Monotonic stack -42 Connect rainwater
Memory search acwing 901 skiing