与所有单词相关联的字串

给定一个字符串 和一些长度相同的单词 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. [bzoj5404]party

    https://zybuluo.com/ysner/note/1240918 题面 这题面不好概括啊 解析 \(5pts\)算法 既然\(q=0\),打上文件输入输出即可. 当然不开够空间且不特判的小 ...

  2. android 中activity 属性说明(转载)

    转自:http://liuwuhen.iteye.com/blog/1759796 activity是android中使用非常平凡的一种组件,我们除了需要掌握activity中的生命周期以外,还需要掌 ...

  3. PID204特种部队

    特种部队   题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达 ...

  4. Python多线程、多进程

    1.from  multiprocessing import Process ;  from  threading import Thread 2.进程之间的数据传输 ,一般会使用到pipes, qu ...

  5. Jquery 《不想工作系列》--整理一下append、prependTo、after的区别

    还有其他类似方法,以后再加,直接上代码和图 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &q ...

  6. Laravel5.1学习笔记13 系统架构5 Contract

    Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...

  7. P1418 选点问题

    题目描述 给出n个点,m条边,每个点能控制与其相连的所有的边,要求选出一些点,使得这些点能控制所有的边,并且点数最少.同时,任意一条边不能被两个点控制 输入输出格式 输入格式: 第一行给出两个正整数n ...

  8. fcc html5 css 练习3

    行内样式看起来是这样的 <h1 style="color: green"> .pink-text { color: pink !important; }         ...

  9. html——表单控件

    基本的表单控件还有html5的一些新的表单控件: <!DOCTYPE html> <html> <head> <meta charset="utf- ...

  10. C# 后台按键 视频播放器 全屏后无法 触发

    第一种  (全屏不可触发) protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Win ...