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 中的单词完全匹配,中间不能 ...
随机推荐
- MATLAB GUI设计(线性卷积和循环卷积的比较--笔记)
原创循环卷积代码,转载需注明出处 线性卷积与循环卷积的比较 实验目的和要求 掌握循环卷积和线性卷积的原理,与理论分析结果比较,加深理解循环卷积与线性卷积之间的关系. 实验内容和步骤 1) 已知两序列X ...
- lumen----------lumen如何安装和使用redis第三方包扩展
1. 安装扩展 要使用redis必须安装两个扩展 "predis/predis": "~1.0", "illuminate/redis" ...
- MongoDB基本信息
一.MongoDB简介 来源:在2007年,由纽约一个叫10gen的创业团队开发,公司现在叫做MongoDB Inc,最初被开发为PAAS(平台即服务). 数据库类型:基于分布式文件存储的数据库.由C ...
- c# 中HttpClient访问Https网站
c# 中HttpClient访问Https网站,加入如下代码: handler = new HttpClientHandler() ;handler.AllowAutoRedirect = true; ...
- Django框架详细介绍---AJAX
一.概述 1.什么是JSON JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * ...
- python数据科学 学习之路
week1 - Python基础1 介绍.基本语法.流程控制 week1- Python基础2 列表.字典.集合 week1- Python基础3 函数.递归.内置函数 week1- Pyt ...
- WdatePicker 日期区间设置
<input id="billsStartDate" name="billsStartDate" onclick="WdatePicker({d ...
- C++ 屏幕录制
http://www.pudn.com/Download/item/id/1584698.html task.renwuyi.com
- 2.2JAVA基础复习——JAVA语言的基础组成运算符和语句
JAVA语言的基础组成有: 1.关键字:被赋予特殊含义的单词. 2.标识符:用来标识的符号. 3.注释:用来注释说明程序的文字. 4.常量和变量:内存存储区域的表示. 5.运算符:程序中用来运算的符号 ...
- 使用aspx 直接生成excel
<%@ Page Language="C#" EnableEventValidation="false" ResponseEncoding="g ...