给定一个字符串 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)的更多相关文章

  1. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  2. 【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 ...

  3. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  4. Leetcode 30 串联所有单词的子串 滑动窗口+map

    见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...

  5. leetcode.字符串.409最长回文串-Java

    1. 具体题目 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设 ...

  6. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  7. Leetcode——30.与所有单词相关联的字串【##】

    @author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...

  8. [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

    30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...

  9. LeetCode(30):与所有单词相关联的字串

    Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

随机推荐

  1. python中pip升级

    第一步: 首先安装python,在百度中搜索python,进入python官网,点击downliad, 下载到本地: 下载好后进行安装,与一般软件安装过程一样.安装过程中要注意勾线Add python ...

  2. Docker容器和本机之间的文件传输

    docker cp 本地文件路径 ID全称:容器路径

  3. PyCharm:no module named * 解决方法

    1.成功安装模块,无法导入 今天安装完模块pyppeteer,pycharm导入失败,从python的Lib下可以清楚的看到已经安装成功 2.添加当前python环境,不使用默认项目的环境 file& ...

  4. Java面试题整理---Redis篇

    1.redis支持五种数据结构类型?   2.redis内部结构?   3.redis持久化机制?   4.redis集群方案与实现?   5.redis为什么是单线程的?   6.redis常见回收 ...

  5. react context跨组件传递信息

    从腾讯课堂看到的一则跨组件传递数据的方法,贴代码: 使用步骤: 1.在产生参数的最顶级组建中,使用childContextTypes静态属性来定义需要放入全局参数的类型 2.在父组件中,提供状态,管理 ...

  6. 根据RadioButtonList动态显示隐藏Div

    使用场景 今天在写项目的时候遇到一个需求,注册页面,用户先选择类型继而填表单,所以需要根据选择切换表单,使用的前端框架是MiniUI,但是在实现这个功能的时候mini.get()方法无法得到div元素 ...

  7. goldengate密码加密

    ----------------ogg加密GGSCI (ogghost) 10> encrypt password goldengate,ENCRYPTKEY defaultUsing defa ...

  8. 【Alpha】Scrum Meeting 11

    目录 前言 任务分配 燃尽图 会议照片 签入记录 前言 第11次会议于4月16日18:15在一公寓三楼召开. 交流确认了任务进度,讨论项目发布事宜,分配下一阶段任务.时长45min. 任务分配 姓名 ...

  9. net应用程序池自动关闭的解决方法

    while (true) { using (ServerManager sm = ServerManager.OpenRemote("localhost")) { string p ...

  10. Bootstrap如何实现导航条?导航条实例详解

    本文主要和大家分享Bootstrap实现导航实例详解,在建设一个网站的时候,不同的页面有很多元素是一样的,比如导航条.侧边栏等,我们可以使用模板的继承,避免重复编写html代码.现在我们打算实现一个在 ...