当前位置:网站首页>[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;
}
边栏推荐
- Scrum和看板的区别
- OpenSSL 编程 一:基本概念
- SQL必需掌握的100个重要知识点:IN 操作符
- win11桌面出现“了解此图片”如何删除
- SQL必需掌握的100个重要知识点:创建计算字段
- 熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
- 小王的面试训练任务
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
- 100 important knowledge points that SQL must master: retrieving data
- Go from entry to practice -- CSP concurrency mechanism (note)
猜你喜欢

win11桌面出現“了解此圖片”如何删除

Process control task

Educational Codeforces Round 108 (Rated for Div. 2)

Null pointer exception

Let Ma Huateng down! Web3.0, hopeless

Go从入门到实战——Context与任务取消(笔记)

∫(0→1) ln(1+x) / (x² + 1) dx

List of language weaknesses --cwe, a website worth learning

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

AI painting minimalist tutorial
随机推荐
100 important knowledge points that SQL must master: combining where clauses
[LeetCode]动态规划解拆分整数I[Silver Fox]
神奇的POI读取excel模板文件报错
Special training of guessing game
Go从入门到实战——CSP并发机制(笔记)
Common methods of string class
数组作业题
Go从入门到实战——仅执行一次(笔记)
分享|智慧环保-生态文明信息化解决方案(附PDF)
Xiao Wang's interview training task
我想我要开始写我自己的博客了。
快递e栈——数组篇小型项目
Go from introduction to actual combat - package (notes)
qt base64加解密
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
100 important knowledge points that SQL must master: filtering data
Go from entry to practice -- CSP concurrency mechanism (note)
[LeetCode]513. 找树左下角的值
[LeetCode]508. 出现次数最多的子树元素和