030 Substring with Concatenation of All Words 与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引,子串要与串联串完全匹配,中间不能有其他字符。
举个例子,给定:
s:"barfoothefoobarman"
words:["foo", "bar"]
你应该返回的索引: [0,9]。(任意顺序)
详见:https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/
Java实现:
class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> res=new ArrayList<Integer>();if(s.isEmpty()||s==null||words==null||words.length==0){return res;}int n=words.length;int m=words[0].length();Map<String,Integer> m1=new HashMap<String,Integer>();for(String str:words){if(m1.containsKey(str)){m1.put(str,m1.get(str)+1);}else{m1.put(str,1);}}for(int i=0;i<=s.length()-n*m;++i){Map<String,Integer> m2=new HashMap<String,Integer>();int j=0;for(;j<n;++j){String t=s.substring(i+j*m,i+j*m+m);if(!m1.containsKey(t)){break;}if(m2.containsKey(t)){m2.put(t,m2.get(t)+1);}else{m2.put(t,1);}if(m2.get(t)>m1.get(t)){break;}}if(j==n){res.add(i);}}return res;}
}
参考:https://www.cnblogs.com/grandyang/p/4521224.html
https://blog.csdn.net/fly_yr/article/details/47957459
转载于:https://www.cnblogs.com/xidian2014/p/8687693.html
030 Substring with Concatenation of All Words 与所有单词相关联的字串相关推荐
- leetcode 30. Substring with Concatenation of All Words 与所有单词相关联的字串 滑动窗口法
题目描述 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. You are given a string, s, and a ...
- LeetCode 30. Substring with Concatenation of All Words
LeetCode 30. Substring with Concatenation of All Words Solution1: 转载自:http://www.cnblogs.com/grandya ...
- Leetcode:Substring with Concatenation of All Words分析和实现
题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...
- 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. ...
- 【leetcode】30. Substring with Concatenation of All Words
题目如下: 解题思路:本题题干中有一个非常关键的前提,就是words中的所有word的长度一样,并且都要被使用到.所以,我们可以把输入的s按word的长度进行等分,以s = "barfoot ...
- LC30 Substring with Concatenation of All Words
这题和LC76 Minimum Window Substring可以归为一类,就是给出要找的字符(字符串)和相应个数,利用滑动窗口在另一个字符串上找.类似的题目还有LC3. 首先需要用map把出现的单 ...
- 30. Substring with Concatenation of All Words
2019独角兽企业重金招聘Python工程师标准>>> 转载于:https://my.oschina.net/datacube/blog/704394
- **LeetCode 30. Substring with Concatenation of All Words
https://leetcode.com/problems/substring-with-concatenation-of-all-words/ 滑动窗口法.解析看这里: http://www.2ct ...
- 【2017.11.30】3. Longest Substring Without Repeating Characters-最长字串不重复字符
3.1sub- 子 substring()子字符串 //javascript omen使用substring()从字符串提取一些字符 <script type="text/javasc ...
最新文章
- 企业ERP制度的“执行力”
- boost::histogram::axis::regular用法的测试程序
- Parse Too complex in xxxx.cpp --------source insight
- php接口,免认证的短信接口,每天单个号码140条以上
- 类似collect2: ld returned 1 exit status的错误
- 真机iOS SDK升级后xcode不能进行真机调试 怎么办
- 最不靠谱CEO,一句话让公司损失上千亿元
- IDEA Maven 聚合项目(多模块)搭建--最精简
- 拓端tecdat|R语言中的岭回归、套索回归、主成分回归:线性模型选择和正则化
- 虚拟机中运行mac系统很卡
- 全国各地网吧网管技术QQ群
- “一线城市,年薪30万+,我却裸辞回老家”一个寒门贵子的10年职业思考
- 苹果充电时一充一停怎么办_苹果充电线一会儿能充一会儿不能充怎么回事
- u8信息服务器,U8开启服务器
- 2020年郑州大学计算机录取分数线,2020年郑州大学各省各专业录取分数线
- 黑马程序——Eclipse 快捷键大全(黑马我来了之一)
- docker下安装apollo多环境(DEV 和UAT)
- 2022-2028年中国破碎筛分设备行业市场发展规模及投资机会分析报告
- Linux5.8下配置web服务器-入门级指导手册(step by step)
- 彻底卸载软件的注册表
热门文章
- 第30课 棋盘上的学问 《小学生C++趣味编程》
- sqlsession.selectlist 会返回null么_如何在Java代码中去掉烦人的“!=null”
- 操作系统课设之Linux 进程间通信
- python字符串_python字符串格式化
- Vue第二部分(4): 嵌套路由和路由传参
- 【分享-windows文件快速搜索神器】Everything 免费、快速搜索文件/文件夹
- C++设计模式-装饰模式
- 系统架构师学习笔记-系统开发基础知识(二)
- Linux学习笔记-编译与链接的过程
- python常用的装饰器库_Python中的各种装饰器详解