给定一个字符串 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. 非系统表空间损坏,rman备份恢复

    实验条件:有完整可用备份--查询表空间情况SQL> select tablespace_name,status from dba_tablespaces;TABLESPACE_NAME STAT ...

  2. pip3 更改安装源

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  3. C/C++文件读写操作总结

    本文主要从两方面介绍读写文件操作,一个是C,另一个是C++. 一.基于C的文件操作. 在ANSI C中对文件操作有两种方式,一种是流式文件操作,另一种是I/O文件操作.下面分别介绍. 1.流式文件操作 ...

  4. WPF实现右键菜单

    ContextMenu类就是用来做右键菜单的对象,对于任何的控件都可以进行对ContextMenu属性的操作进行设置右键菜单的功能. 下面代码就是对一个按钮添加一个WPF右键菜单的功能: < B ...

  5. JS ES6 -- let命令

    1.ES6新增了块级作用域的let和const 这新特性let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. for循环的计数器,就很合适使用let命令 ...

  6. win10系统-javac不是内部或外部命令

    给笔记本装了一个ssd,上午装的系统,重新搞jdk,设置JAVA_HOME之后,cmd运行javac报 “javac不是内部或外部命令”各种懵逼,试了好几次才发现Path路径里面不能用%JAVA_HO ...

  7. 【问题】Expandable数据集的定义的正确方法,TabActivity弃用替代,Gallery替代,imageswitcher

    Expandable 问题: http://www.cnblogs.com/xingyyy/p/3389611.html 扩展阅读:http://blog.csdn.net/lmj623565791/ ...

  8. php学习笔记-可变变量

    看一个例子. <?php $a = 'hello'; $hello = 'hi'; echo $$a; ?> 如果一个变量名前面有两个美元符号,那么这个变量就叫做可变变量.就拿上面这个举例 ...

  9. iOS开发中,修改ASIHTTPRequest源码,禁止在POST时URL编码

    通过ASIHTTPRequest库进行POST时,会对POST的文本内容进行encodeURL,而且ASIHTTPRequest自身并没有配置项可以关闭这个转换. 本文提供一个方法关闭encodeUR ...

  10. supervisor启动worker源码分析-worker.clj

    supervisor通过调用sync-processes函数来启动worker,关于sync-processes函数的详细分析请参见"storm启动supervisor源码分析-superv ...