[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 ...
随机推荐
- jar包不能乱放【浪费了下午很多时间】
不能放在类路径下(也即是src文件夹下),然后再buildpath 必须放在web-inf文件夹下 这样才能tomcat找打jar文件
- cookie mapping 原理理解
深入浅出理解 COOKIE MAPPING Cookie mapping技术 利用javascript跨域访问cookie之广告推广
- 【WebLogic使用】1.WebLogic的下载与安装
一.WebLogic的介绍 WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogic ...
- 串口接收端verilog代码分析
串口接收端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...
- ubuntu10.04 32 编译android源码的问题
ubuntu10.04 32 问题 没有jdk1.6 并且使用apt-get 安装jdk相当麻烦,参照:http://blog.csdn.net/godvmxi/article/detail ...
- 【疑难杂症】gdb调试多线程程序报错:interrupted system call
一. cmake生成可调试版本的程序,该内容参考自https://www.linuxidc.com/Linux/2014-03/98622.htm 具体内容如下: 1, 使用CMAKE编译确实很方便. ...
- php调用API支付接口 可个人使用,无需营业执照(使用第三方接口,调用的天工接口。)
首先访问 https://charging.teegon.com/ 注册账号, 找到开发配置 记下client_id和client_secret. 点击 天工开放平台 点击天工收银 点击 S ...
- 廖雪峰Java7处理日期和时间-4最佳实践-最佳实践
jdk提供了2套新旧的API来处理日期和时间. java.util * Date * Calendar java.time(JDK>=1.8) * Localdate * LocalTime * ...
- Xtrabackup2.4.8备份、还原、恢复Mysql5.7.19实操(网络拷贝)
环境:CentOS 6.7 + Mysql 5.7.19 + Xtraback 2.4.8 innobackupex常用参数: --user=USER 指定备份用户,不指定的话为当前系统用户 --p ...
- vm虚拟机 模板机进行克隆导致centos 7.2 无法加载网卡
问题描述:vm虚拟机 模板机进行克隆导致centos 7.2 无法加载网卡. 1.ifconfig 查看网卡状态 lo: flags=<UP,LOOPBACK,RUNNING> mtu i ...