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由所有单词连成的子串的更多相关文章

  1. [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 ...

  2. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

    题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description   在字符 ...

  7. [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 ...

  8. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  9. 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 ...

随机推荐

  1. PythonStudy——魔法函数 Magic Methods

    魔法函数 python中以双下划线开始和结束的函数(不可自己定义)为魔法函数 调用类实例化的对象的方法时自动调用魔法函数(感觉不需要显示调用的函数都叫) 在自己定义的类中,可以实现之前的内置函数,比如 ...

  2. Twitter的雪花算法(snowflake)自增ID

    前言 这个问题源自于,我想找一个分布式下的ID生成器. 这个最简单的方案是,数据库自增ID.为啥不用咧?有这么几点原因,一是,会依赖于数据库的具体实现,比如,mysql有自增,oracle没有,得用序 ...

  3. flask-appbuilder +echarts 展示数据笔记

    pip install flask-appbuilder fabmanager create-app cd newapp fabmanager create-admin fabmanager run ...

  4. 对poi-excel导出的浅层理解

    上一篇对excel导入做了浅层的解释,本文将对导出再做浅层解释. 仍然是相同的套路,只不过是反过来而已. 反过来方向理论上本来是这样的:cell-->row-->sheet-->wo ...

  5. TCP/IP学习20180701-数据链路层-IP子网寻址

    IP-子网寻址IP地址是:网络号+主机号现在主机号都要求有子网号所以IP地址就变成了网络号+子网号+主机号例如一个B类地址:210.30.109.134210.30是网络号109是子网号134是主机号 ...

  6. k8s Nodeport方式下service访问,iptables处理逻辑(转)

    原文 https://www.myf5.net/post/2330.htm k8s Nodeport方式下service访问,iptables处理逻辑 2017年07月11日 0条评论 976次阅读 ...

  7. inode引起的Linux无法创建新文件,磁盘空间不足

    df -h,判断硬盘空间是否已经满了,占用率达100% ,就可以断定该分区满了. df -ia,占用率达100%,也会导致无法创建新文件.一般都是存在大量小文件引起的. inode包含文件的元信息,具 ...

  8. python之路——11

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 一.装饰器 1.时间模块 time.time time.sleep 2.装饰器 原则---开放封闭 ...

  9. JVM学习总结(一):Java内存区域

    一.JVM运行时数据区 1.程序计数器: (1)一块较小的线程私有的内存空间. (2)JVM的多线程是通过线程轮流切换并分配处理器执行时间的方式来实现的,在任何一个确定的时刻,一个处理器(或一个内核) ...

  10. docker network基础

    前面介绍了nginx与php两个容器间是如何进行通信的: [root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html ...