LeetCode--030--串联所有单词的字串(java)
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:
s = "barfoothefoobarman",
words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
输出:[]
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
if(s == null || words == null || words.length == 0)return new ArrayList<>();
List<Integer> res = new ArrayList<>();
int n = words.length;
int m = words[0].length();
HashMap<String,Integer> map = new HashMap<>();
for(String str: words){
map.put(str,map.getOrDefault(str,0)+1);
}
for(int i = 0;i <= s.length() - n * m;i++){
HashMap<String,Integer> copy = new HashMap<>(map);
int k = n;
int j = i;
while(k > 0){
String str = s.substring(j,j+m);
if(!copy.containsKey(str) || copy.get(str) < 1){
break;
}
copy.put(str,copy.get(str) - 1);
k--;
j += m;
}
if(k == 0)res.add(i);
}
return res;
}
}
2019-04-23 17:31:31
LeetCode--030--串联所有单词的字串(java)的更多相关文章
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- 【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. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- Leetcode 30 串联所有单词的子串 滑动窗口+map
见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...
- leetcode.字符串.409最长回文串-Java
1. 具体题目 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设 ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- Leetcode——30.与所有单词相关联的字串【##】
@author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...
- [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)
30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...
- LeetCode(30):与所有单词相关联的字串
Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
随机推荐
- Spring Security 内置过滤器表
别名 Filter 类 CHANNEL_FILTER ChannelProcessingFilter SECURITY_CONTEXT_FILTER SecurityContextPersistenc ...
- php----------const 定义的常量和define()定义的常量的区别?
用法一:const用于类成员变量,一经定义不可修改,define用于全局常量,不可用于类成员变量的定义,const可在类中使用也可以在类外面使用,define不能. 定义:const 常量名=值; 没 ...
- 微信小程序之回调函数
在微信小程序中众所周知在js里面得方法都是异步执行,我最近再做项目得时候也遇到了这个问题,再方法里面调用另一个方法里面的接口数据,第一次是调取不到的, 因为两个方法是同时开始执行得,所以怎么都取不到值 ...
- linux下chromedriver的安装
很多时候,发现phantomjs被一些网站屏蔽导致我们无法达到想要的结果,一方面phantomjs也停止维护,这时候们可以使用chromedriver(谷歌)或者firefoxdriver(火狐)来代 ...
- PostgreSQL 9.5.x的架构图及外存图
- 机器学习算法GBDT
http://www-personal.umich.edu/~jizhu/jizhu/wuke/Friedman-AoS01.pdf https://www.cnblogs.com/bentuwuyi ...
- spring里的事物设置
有的人说事物在spring里设置有两种,其实事物设置在spring配置文件中共有五种方式:第一种方式:每个Bean都有一个代理第二种方式:所有Bean共享一个代理基类第三种方式:使用拦截器第四种方式: ...
- flask 异步发送邮件
异步发送邮件 当使用SMTP的方式发送电子邮件时,如果你手动使用浏览器测试程序的注册功能,在提交注册表单后,浏览器会有几秒钟的不响应.因为这时候程序正在发送电子邮件,发信的操作阻断了请求--响应循环, ...
- 安卓BLE测试apk
安卓蓝牙测试APP 文件下载:蓝牙测试.apk
- 聊天斗图神器aidou mac中文版
aidou mac中文版是mac上一款专为斗图设计的聊天斗图神器,让您在聊天的时候不怕没有表情用,炫酷的表情即时更新,让您每时每刻都有新表情用!一键下载的表情包!小编现为您带来aidou斗图神器免费版 ...