[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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
Example 1:
Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
Output: []
题意:
给定一个无重复单词的字典D,和一个长字符串S。找出S中的子串,该子串恰好是D中所有单词连接而成。
code
/*
Time: O(n * m ). outter for loop to scan n items, inner for loop to scan m substrings
Space: O(m)
*/
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
// corner case
if (words.length == 0 || s.length() == 0) return result; int wordLength = words[0].length();
int catLength = wordLength * words.length; // 求Concatenation长度。 因为题干说words中每个单词长度一致。
// corner case
if (s.length() < catLength) return result; Map<String, Integer> map = new HashMap<>();
for (String word : words)
map.put(word, map.getOrDefault(word, 0) + 1); // words中有单词可能出现多次 // 终结到s.length() - catLength因为最后一部分catLength长度的串可能是一个valid Concatenation解
for (int i = 0; i <= s.length() - catLength; ++i) {
// deep copy
Map<String, Integer> checkingMap = new HashMap<>(map); for (int j = i; j < i + catLength; j = j + wordLength) {
final String key = s.substring(j, j + wordLength);
final int freq = checkingMap.getOrDefault(key, -1); if (freq == -1 || freq == 0) break; checkingMap.put(key, freq - 1);
if (freq - 1 == 0) checkingMap.remove(key);
} if (checkingMap.size() == 0) result.add(i);
}
return result;
}
}
[leetcode]30. Substring with Concatenation of All Words由所有单词连成的子串的更多相关文章
- [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
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
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 (words中全部子串相连) 解题思路和方法
Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...
- Java [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 a ...
- LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description 在字符 ...
- [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][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- 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 ...
随机推荐
- PythonStudy——逻辑运算符 Logical Operators
在Python中,None.任何数值类型中的0.空字符串“”.空元组().空列表[].空字典{}都被当作False,还有自定义类型,如果实现了 __ nonzero __ () 或 __ len __ ...
- visio画图有感
昨天在和一个同事看流程图,在我还在考虑图的含义时他说这个图太乱了,如果要团队成员看也会很费劲,并找出觉得画的好的案例. 对比两个图我发现了一个最大的差别是好的图形状都是水平或垂直对齐的,连接线也都是水 ...
- Tomcat性能调优后, 启动出现警告问题 [did not find a matching property.]
http://blog.csdn.net/dracotianlong/article/details/8963594 Tomcat性能调优后, 启动出现警告问题 [did not find a mat ...
- python之路——8
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 .1.文件操作 笔记.txt 1.文件路径:D:\python\Day8\笔记.txt 2.编码方 ...
- mysql修改root用户的登录密码
修改mysql root用户登录密码的方法有很多,网上可以查找到相关的资料 我通过上网查询以后实验成功的方法是使用下面的sql语句进行修改 UPDATE user SET Password = PAS ...
- linux文件压缩解压命令
01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩! ...
- jenkins-1
1 下载jenkins,https://jenkins.io/download/, 我在此处用的是war的的形式启动的,配置tomact的server.xml,如果是一个主机多个tomact的话还要编 ...
- 普通PC机支持内存128G,单条32G内存
以前,不管是英特尔还是AMD的消费级平台支持内存容量大多都是64GB,这一现状被英特尔公司去年推出的第九代酷睿而改变.第九代酷睿最大支持128GB内存,虽然只是简单的提高了内存容量,对大多数电脑用户而 ...
- feign接口调用异常的解决方向
1. consul: 检查调用方服务与被调用方服务是否在同一个consul; 2. swagger: 检查swagger注释是否清晰.恰当: 比如: @ApiImplicitParams(value ...
- Android 开发 Handler的基本使用
转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/72677227 深入理解Handler.Looper.Messagequeue ...