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> ...
随机推荐
- bzoj4756
http://www.lydsy.com/JudgeOnline/problem.php?id=4756 水题一枚...但是我写了一个小时...手贱打反查不出来... 就是每次线段树合并,先把自己的儿 ...
- E20170617-hm
notation n. 记号,标记法; implicit adj. 不言明[含蓄]的; 无疑问的,绝对的; 成为一部份的; 内含的; selector n. 选择者,选择器; promot ...
- E20170626-hm
authenticate vt. 认证,证明是真实的.可靠的或有效的; 鉴定,使生效; author n. 作者; 著作家; 创造者; 发起人;
- 题解报告:hdu 1272 小希的迷宫
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem ...
- bootstrap图标乱码问题-解决方案
楼主在使用bootstrap时,出现了图标乱码问题,经过多次查找,才解决了问题(最后发现真的是很好解决的问题(^^)) 如果出现乱码 请直接在自己写的CSS中重新引入一下font文件中的字体就好了 @ ...
- Html基础学习(基于W3school网络教程)
//防止乱码 head中加入 <meta http-equiv="Content-Type" content="text/html; charset=utf-8&q ...
- React Native 环境搭建踩坑
React Native (web Android)环境搭建踩坑(真的是一个艰辛的过程,大概所有坑都被我踩了 官方文档地址 : https://facebook.github.io/react-nat ...
- 客户端通过base64上传bitmap服务器
首先致谢:http://www.jb51.net/article/129743.htm 咱们不是代码的生产者,只是代码的搬运工. 场景描述:Android客户端需要上传头像等图片到服务器,经双方协商决 ...
- 移动web——媒体查询
基本概念 响应式开发在没有媒体查询前,也可以通过js来实现,但是人们基本不会考虑,特别繁琐.在出现了媒体查询,才开始逐渐推广响应式.实际开发中,在时间与金钱充足的情况下还是别做响应式,影响性能,维护麻 ...
- html——导航demo
通过行内块.伪类对导航栏进行设置 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...