与所有单词相关联的字串

给定一个字符串 和一些长度相同的单词 words。 s 中找出可以恰好串联 words 中所有单词的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

示例 1:

输入:

s = "barfoothefoobarman",

words = ["foo","bar"]

输出: [0,9]

解释: 从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。

输出的顺序不重要, [9,0] 也是有效答案。

题目的意思是给你一个字符串,和一个字符串的数组,需要返回一个该字符串的索引组成的数组,返回的索引有如下性质:从每个索引开始,长度为L的字串需要精确包含字符串数组中的所有字符串(不多不少)。L 为字符串数组中所有字符串长度之和。

解决思路,使用一个map,键为字符串数组中的字符串,值为该字符串在字符串数组中出现的次数。遍历字符串s,寻找和字符串数组中的字符串相同的字串,找到后map中的值减一,否则重新初始化map,从下一个字符开始遍历。如果map中所有的值都为0,则找到了一个符合条件的子串,索引压入数组。

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; class Solution {
public static void initializeMap(HashMap<String,Integer> hashMap, String[] words){
//hashMap=new HashMap<String,Integer>();
for(int i=0;i<words.length;i++){
if(!hashMap.containsKey(words[i])){
hashMap.put(words[i],1);
}else{
hashMap.put(words[i],hashMap.get(words[i])+1);
}
}
}
public static List<Integer> findSubstring(String s, String[] words) {
if(s==null || s.equals("") || words.length==0) return new ArrayList<Integer>();
HashMap<String,Integer> hashMap=new HashMap<String,Integer>();
int singleWordLen=words[0].length();//单个字串长度
int wordsLen=words.length;
int slen=s.length();
int i,j,count;
boolean countChange=false;//判断是否改变过map中的值,如果没有变则无需重新初始化
List<Integer> result=new ArrayList<Integer>();
count=wordsLen;//计数器表示还需要找到的字串个数
if(wordsLen==0 || slen==0) return result;
initializeMap(hashMap,words);
for(i=0;i<=slen-wordsLen*singleWordLen;i++){
String subStr=s.substring(i,i+singleWordLen);
j=i;
//当该字串存在于map中且值大于0,并且j不越界的情况下
while(hashMap.containsKey(subStr) && hashMap.get(subStr)!=0 && j+singleWordLen<=slen){
hashMap.put(subStr,hashMap.get(subStr)-1);
count--;
countChange=true;//改变了map的值
j=j+singleWordLen;
if(j+singleWordLen<=slen)
subStr=s.substring(j,j+singleWordLen);//下一个字符串
else
break;
if(!hashMap.containsKey(subStr))
break;
}
if(count==0){
result.add(i);//找齐所有字符串数组中的字串后把该索引压入
}
if(countChange){
hashMap.clear();
initializeMap(hashMap,words);
count=wordsLen;
countChange=false;
}
}
return result;
} public static void main(String[] args){
String s="wordgoodgoodgoodbestword";
String[] words={"word","good","best","good"};
List<Integer> list=findSubstring(s,words);
for(int i:list){
System.out.println(i);
}
}
}

Leetcode 30.与所有单词相关联的子串的更多相关文章

  1. [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

    30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...

  2. Leetcode——30.与所有单词相关联的字串【##】

    @author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...

  3. 30. 与所有单词相关联的字串、java实现

    题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符, ...

  4. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  5. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  6. LeetCode(30):与所有单词相关联的字串

    Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

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

  8. 030 Substring with Concatenation of All Words 与所有单词相关联的字串

    给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...

  9. Leetcode 30 串联所有单词的子串 滑动窗口+map

    见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...

随机推荐

  1. codevs1312连续自然数和

    1312 连续自然数和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 对于一个自然数M,求出所有的连续的自然数段 ...

  2. Expected one result (or null) to be returned by selectOne(), but found: 2 和 java.lang.UnsupportedOperationException异常

    在学习MyBatis的时候,简简单单的MyBatis+MySql的增删改查操作,但是却出了问题. 刚开始数据库只有一条数据的时候,岁月静好,一切看起来都那么的OJBK.但是,当我往数据库插入第二条数据 ...

  3. HTML--使用下拉列表框进行多选

    下拉列表也可以进行多选操作,在<select>标签中设置multiple="multiple"属性,就可以实现多选功能,在 widows 操作系统下,进行多选时按下Ct ...

  4. day03_12/13/2016_bean的管理之作用域与初始化时间

    在Spring中,Bean有几种作用域: 1.singleton作用域 当一个bean的作用域设置为singleton,那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean ...

  5. [转]Linux命令wc的详细用法

    转自:http://blog.hehehehehe.cn/a/17301.htm wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, word ...

  6. Android 自己搭建一个直播系统吧

    服务端用 SRS(Simple Rtmp Server),在这里下载simple-rtmp-server需要Linux系统最好是Ubuntu,装个Ubuntu虚拟机就行了在Linux里,解压缩SRS ...

  7. [Android]异常1-duplicate files during packaging of

    异常原因: 可能一>项目存在重复资源文件 可能二>Android Studio与源代码Android Studio不一致 解决方法有: 解决一>查找重复资源,删除或者修改文件 解决二 ...

  8. STL之set篇

    insert为插入.set_intersection求交集,set_union求并集,是属于algorithm里的函数. 例题有 PAT甲级1063 #include<iostream> ...

  9. Miller Rabin 大素数测试

    PS:本人第一次写随笔,写的不好请见谅. 接触MillerRabin算法大概是一年前,看到这个算法首先得为它的神奇之处大为赞叹,竟然可以通过几次随机数据的猜测就能判断出这数是否是素数,虽然说是有误差率 ...

  10. VMWare 安装Ubuntu 16.04

    1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...