当前位置:网站首页>[LeetCode]30. Concatenate substrings of all words
[LeetCode]30. Concatenate substrings of all words
2022-06-27 21:50:00 【A Fei algorithm】
subject
30. Concatenate the substrings of all words
Given a string s And some of the Same length 's words words . find s It happens that words The starting position of the substring formed by concatenation of all words in .
Pay attention to the relationship between the string and words The words in match exactly , No other characters in the middle , But there's no need to think about words The order in which words are concatenated .
Example 1:
Input :s = "barfoothefoobarman", words = ["foo","bar"]
Output :[0,9]
explain :
From the index 0 and 9 The first substrings are "barfoo" and "foobar" .
The order of output is not important , [9,0] It's also a valid answer .
Example 2:
Input :s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output :[]
Example 3:
Input :s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output :[6,9,12]
Tips :
1 <= s.length <= 104
s It's made up of lowercase letters
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i] It's made up of lowercase letters
Method 1: Hash + Compare
public List<Integer> findSubstring(String s, String[] words) {
Map<String, Integer> dict = new HashMap<>();//words Dictionary map,k Is the current word word v Is the number of occurrences
int len = 0, w_len = 0; // The total length , When the length of a word , The length of each word is equal , Just record one
for (String word : words) {
len += word.length();
w_len = word.length();
dict.put(word, dict.getOrDefault(word, 0) + 1);
}
int n = s.length();
List<Integer> res = new ArrayList<>();
for (int i = 0; i + len - 1 < n; i++) {
Map<String, Integer> can = new HashMap<>();// Candidate map
String sub = s.substring(i, i + len);
// System.out.println(sub);
// Every time w_len Segmentation of words in steps of
for (int j = 0; j < len; j += w_len) {
String seg = sub.substring(j, j + w_len);
can.put(seg, can.getOrDefault(seg, 0) + 1);
}
//dict And can In unison , Description can form
if (dict.equals(can)) {
res.add(i);
}
}
return res;
}
边栏推荐
- Null pointer exception
- After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
- ABC-Teleporter Setting-(思维+最短路)
- 动态刷新mapper看过来
- Go从入门到实战——依赖管理(笔记)
- Go从入门到实战——所有任务完成(笔记)
- TreeSet details
- SQL必需掌握的100个重要知识点:排序检索数据
- linux下安装oracle11g 静默安装教程
- Go从入门到实战—— 多路选择和超时控制(笔记)
猜你喜欢

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

Go从入门到实战——package(笔记)

Knowledge sorting of exception handling

洛谷P5706 再分肥宅水

SQL必需掌握的100个重要知识点:过滤数据

Go从入门到实战——共享内存并发机制(笔记)

猜拳游戏专题训练

AI painting minimalist tutorial

After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"

Go从入门到实战——Context与任务取消(笔记)
随机推荐
SQL必需掌握的100个重要知识点:IN 操作符
oracle迁移mysql唯一索引大小写不区分别怕
[LeetCode]动态规划解分割数组I[Red Fox]
Go从入门到实战——Panic和recover(笔记)
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
GBase 8a OLAP分析函数cume_dist的使用样例
Tiktok's interest in e-commerce has hit the traffic ceiling?
[LeetCode]515. 在每个树行中找最大值
根据自定义excel标题模板快速excel导出
创建对象时JVM内存结构
Go从入门到实战——接口(笔记)
Educational Codeforces Round 108 (Rated for Div. 2)
Burp suite遇到的常见问题
Simulink导出FMU模型文件方法
AI 绘画极简教程
Quick excel export according to customized excel Title Template
TypeScript学习
100 important knowledge points that SQL must master: retrieving data
[LeetCode]572. 另一棵树的子树