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
030 Substring with Concatenation of All 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 ...
- LeetCode 030 Substring with Concatenation of All Words
题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- leetcode python 030 Substring with Concatenation of All Words
## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...
- [LeetCode] 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 ...
- [leetcode]30. 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 ...
- [LeetCode] 30. 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 ...
- Longest Substring Without Repeating Characters,求没有重复字符的最长字串
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode HashTable 30 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 ...
随机推荐
- Oracle 12c 多租户 CDB 与 PDB之 shared undo 与 Local undo 切换
undo 在12C R1版本中只支持Global Shared Undo模式, 所有container共享一个UNDO表空间, 目前保留这种模式只是为了升级过渡, 在12C R2引入了PDB Loca ...
- 最常见的5个导致 RAC 实例崩溃的问题
适用于: OracleDatabase - Enterprise Edition - 版本11.2.0.1 和更高版本本文档所含信息适用于所有平台 用途 本文档的目的是总结可能导致 RAC 实例崩溃的 ...
- 【转】 Pro Android学习笔记(六九):HTTP服务(3):HTTP POST MultiPart
目录(?)[-] 建立测试环境 开发环境导入第三方JAR HTTP Post Multipart小例子 HTTP POST不仅可以通过键值对传递参数,还可以携带更为复杂的参数,例如文件.HTTP Po ...
- WPF Canvas
Canvas为容器控件,用于定位. 1.基本应用 <Border HorizontalAlignment="Left" VerticalAlignment="Top ...
- Shape和 layer-list
shape 基本使用 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= ...
- Shrio00 Shiro角色授权、Shiro权限授权、开启Shiro缓存
1 需求01 用户进行过认证登录后,某些接口是有权限限制的:如何实现只有相应权限的用户才可以调用相应接口 2 修改shiro配置类 ShiroConfiguration package cn.xia ...
- SpringCloud01 服务提供者和消费者
说明:服务消费者直接利用RestTemplate调用服务提供者,这种使用方式只是适用于微服务数量比较少的项目,如果微服务的数量比较多建议使用SpringCloud提供的Eureaka组件. 注意:实现 ...
- mysql 表名作为存储过程变量
mysql默认不支持表名作为变量名,如下所示 delimiter $$ DROP procedure IF EXISTS getDataByDbName $$ CREATE procedure get ...
- 第五课5、ROS客户端2
1.简单的主题(topic)发布者和主题订阅者: 编写主题发布者节点需要: a.初始化ROS系统: b.广播消息:在foo主题上发布Foo_type_msg类型的消息 c.指定频率发布消息到foo主题 ...
- 蚂蚁金服HR电话面
1.先自我介绍一下. 2.问到项目问题.(项目介绍,用到哪些技术,如何测试的.) 3.一些基础知识. a) list.map.set是继承了collection的吗? List:1.可以允许重复的对 ...