Leetcode 30.与所有单词相关联的子串
与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 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.与所有单词相关联的子串的更多相关文章
- [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)
30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...
- Leetcode——30.与所有单词相关联的字串【##】
@author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...
- 30. 与所有单词相关联的字串、java实现
题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能有其他字符, ...
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- [LeetCode] 30. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- LeetCode(30):与所有单词相关联的字串
Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 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 ...
- 030 Substring with Concatenation of All Words 与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...
- Leetcode 30 串联所有单词的子串 滑动窗口+map
见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...
随机推荐
- return在Java中的两个作用
Java中return的两个作用: (1)返回方法指定类型的值(这个值总是确定的),也可以是对象 (2)方法的结束
- javaBean注意事项
1.重写tostring方法 2.属性第一位小写
- P3092 [USACO13NOV]没有找零No Change 状压dp
这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了. 题干: 题目描述 Farmer John <= K <= ), each with value .., ...
- codevs3002石子归并3(四边形不等式优化dp)
3002 石子归并 3 参考 http://it.dgzx.net/drkt/oszt/zltk/yxlw/dongtai3.htm 时间限制: 1 s 空间限制: 256000 KB 题目等级 ...
- IE下元素设置百分比的问题
场景:近两天在做一个控件,该控件是一个tab型的,并且该tab有可能是两个tab标签,也有可能是多个tab标签,为了能够适应这种动态需求, 在设置标签宽度的时候,直接用的最外层容器除以tab的个数,然 ...
- Aspose Cells dll 实现数据简单下载
Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[ ...
- 兼容浏览器 div固定浏览器窗口底部 浮动div
css内容: <style type="text/css"> #ken_BB { padding-right:30px; text-align: center; col ...
- HeadFirst HTML&CSS
CH1 认识HTML HTML和CSS是我们用来创建网页的语言:HTML是超文本标记语言(HyperText Markup Language)的缩写,用来建立网页的结构:CSS是层叠样式表(Casca ...
- 如何让win32 c++窗口不出现在任务栏
把窗口作为某一个窗口的子窗口,然后设置WS_POPUP就可以了.使用CreateWindow时的第三个参数设置为WS_CHILD|WS_POPUP.
- Angular——数据绑定
基本介绍 angularjs可以实现数据的双向绑定:(1)视图到模型的数据绑定,(2)模型到数据的绑定 基本使用 1.ng-model可以实现视图到模型的数据传输 2.{{name}}可以实现模型到视 ...