给定一个字符串 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 与所有单词相关联的字串的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. leetcode python 030 Substring with Concatenation of All Words

    ## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. Longest Substring Without Repeating Characters,求没有重复字符的最长字串

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  9. 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 ...

随机推荐

  1. HBase的bulkLoad

    HBase的BulkLoad有两种方式: thinrow的机制是flatmap把cell的信息进行flatmap:适合少于1万列的数据集:thinrow的涵义就是少行多列: bulkload的机制则是 ...

  2. bzoj 1101 Zap —— 莫比乌斯反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 直接莫比乌斯反演. 代码如下: #include<cstdio> #inc ...

  3. MYSQL主从复制配置遇到的问题

    在进行配置从服务器时遇到的错误. mysql> change master to master_host='192.168.136.129',master_user='repl',master_ ...

  4. 三、使用maven创建scala工程(scala和java混一起)

    本文先叙述如何配置eclipse中maven+scala的开发环境,之后,叙述如何实现spark的本地运行.最后,成功运行scala编写的spark程序. 刚开始我的eclipse+maven环境是配 ...

  5. rails常用命令示例

    数据迁移命令 1.一下命令执行后会在db\migrate下产生同名数据迁移文件(文件内容可自行修改,基本语法见“数据迁移文件”部分) 创建model:rails generate model user ...

  6. maven仓库的管理_Nexus

    maven仓库管理的软件有很多,这里介绍的是Sonatype的nexus 一.下载 下载地址:https://yunpan.cn/cv2JhzwQuvb7B  访问密码 932d 二.安装 2.1.将 ...

  7. AngularJs(Part 7)--Build-in Directives

    Directives In AngularJS, we can use a variety of naming conventions to reference directives . In the ...

  8. 【机器学习】关联规则分析(一):Apriori

    一.Apriori原理 Apriori是关联分析中较早的一种方法,主要用来挖掘那些频繁项集合,其思想是: 1.如果一个项目集合不是频繁集合,那么任何包含它的项目(超集)也一定不是频繁集. 2.如果一个 ...

  9. 1.4 DVWA亲测XSS漏洞

    首先需要有配置好的DVWA环境,像下图这样   其中: XSS (DOM) :  DOM型XSS漏洞 XSS (Reflected) : 反射性XSS漏洞  XSS (Stored) :  存储型XS ...

  10. Extjs知识点汇总

    自定义渲染单元格内容 { name:"device.flag", header: '确认', dataIndex: 'flag', width:50, renderer: func ...