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> ...
随机推荐
- Django day26 初识认证组件
一:什么是认证组件 只有认证通过的用户才能访问指定的url地址,比如:查询课程信息,需要登录之后才能查看,没有登录,就不能查看,这时候需要用到认证组件 二:认证组件源码分析
- String,StringBuffer和StringBuilder
在执行速度方面的比较:StringBuilder > StringBuffer StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操 ...
- Android内存管理(12)*「实例」用Monitor 生成.hprof文件 并分析内存泄漏
参考 http://blog.csdn.net/xiaanming/article/details/42396507 基本步骤: 1,准备一个有内存泄漏的代码 2,如何发现内存泄漏 3,生成.hpro ...
- cloudera-scm-server启动时出现Caused by: java.io.FileNotFoundException: /var/lib/cloudera-scm-server/.keystore (No such file or directory)问题解决方法(图文详解)
不多说,直接上干货! 问题详情 查看/var/log/cloudera-scm-server.log的启动日志 问题来源 我在用cloudermanager安装好之后,然后,在对如下. 配置kerbe ...
- spring boot打包文件后,报错\No such file or directory
现象: 一段代码: ClassLoader loader = XXXUtil.class.getClassLoader(); String jsFileName = loader.getResourc ...
- HTML 表格与表单 个人简历
<title>个人简历</title></head> <body background="1e30e924b899a9015b946ac41f950 ...
- <stddef.h>
Common definitions 定义类型: ptrdiff_t 两指针相减的结果,signed integer size_t sizeof操作符的结果,unsigned integer max_ ...
- Mongodb——文档数据库
mongodb是一个文档数据库. mongo操作 多个修改操作,但每个修改携带的数据包较小,可操作考虑批量操作.bulkWrite()改善性能. MongoCollection是线程安全的. db.c ...
- html5——3D案例(音乐盒子、百度钱包)
1.音乐盒子:触碰盒子,盖子会打开 2.百度钱包:触碰钱包,钱包,会180度旋转 <!DOCTYPE html> <html lang="en"> < ...
- [Windows Server 2003] 安装SQL Server 2005
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装SQL S ...