030 Substring with Concatenation of All Words 与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引,子串要与串联串完全匹配,中间不能有其他字符。
举个例子,给定:
s:"barfoothefoobarman"
words:["foo", "bar"]
你应该返回的索引: [0,9]。(任意顺序)
详见:https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/
Java实现:
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res=new ArrayList<Integer>();
if(s.isEmpty()||s==null||words==null||words.length==0){
return res;
}
int n=words.length;
int m=words[0].length();
Map<String,Integer> m1=new HashMap<String,Integer>();
for(String str:words){
if(m1.containsKey(str)){
m1.put(str,m1.get(str)+1);
}else{
m1.put(str,1);
}
}
for(int i=0;i<=s.length()-n*m;++i){
Map<String,Integer> m2=new HashMap<String,Integer>();
int j=0;
for(;j<n;++j){
String t=s.substring(i+j*m,i+j*m+m);
if(!m1.containsKey(t)){
break;
}
if(m2.containsKey(t)){
m2.put(t,m2.get(t)+1);
}else{
m2.put(t,1);
}
if(m2.get(t)>m1.get(t)){
break;
}
}
if(j==n){
res.add(i);
}
}
return res;
}
}
参考:https://www.cnblogs.com/grandyang/p/4521224.html
https://blog.csdn.net/fly_yr/article/details/47957459
030 Substring with Concatenation of All Words 与所有单词相关联的字串的更多相关文章
- [Swift]LeetCode30. 与所有单词相关联的字串 | Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- LeetCode 030 Substring with Concatenation of All Words
题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- leetcode python 030 Substring with Concatenation of All Words
## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...
- [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- [leetcode]30. Substring with Concatenation of All Words由所有单词连成的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- Longest Substring Without Repeating Characters,求没有重复字符的最长字串
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode HashTable 30 Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
随机推荐
- UML Design Via Visual Studio-Class Diagram
用过几个建模设计工具,小的有staruml,大的有rational rose,EA.最后发现还是Visual Studio建模比较舒服(个人观点,不要争论). 打算对自己经常用的几个建模图做一个介绍, ...
- BZOJ1455:罗马游戏
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1455 浅谈左偏树:https://www.cnblogs.com/AKMer/p/102466 ...
- 关于使用C# 启动msi失败的问题
原以为在启动msi是件小儿科的事,上代码: ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "C:\\myTest ...
- VisualGDB系列6:远程导入Linux项目到VS中
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何将Linux机器上的Linu ...
- inner join ,left join ,right join区别
inner join ,left join ,right join区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中 ...
- LAMP 1.4 PHP编译安装
1.下载 ...
- [转载][效率工具推荐]Mathpix – 将图片数学公式转换为 LaTeX
转自小众软件:Mathpix – 将图片数学公式转换为 LaTeX Mathpix 是一款跨平台(Windows.macOS.Linux)的 OCR 工具,它能够识别复杂的数学公式,并将其转换为 La ...
- sort命令实战
本文参考:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html 东方雨中漫步者 sort命令,帮助我们依据不同的数据类型进 ...
- isPCR安装
isPCR是用一对PCR引物搜索序列数据库.它使用索引策略来快速完成此操作.当搜索成功时,输出是fasta格式序列文件,其包含数据库中位于引物对之间的所有区域. Linux系统下安装 1. 使用二进制 ...
- [MySQL] Data too long for column 'title' at row 1
李刚轻量级JavaEE第六章的坑..艹李刚自己有没试过这些代码的啊,6.4这一份HqlQuery.java里需要的表,根本就跟他提供的sql脚本对不上啊..坑爹啊,而且字符编码集也有问题. 出现这个原 ...