[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 starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
问题:给定一个字符串 s 和 一列单词 words 。找出 s 中所有满足下面条件的全部子字符串的开始下标:要求子字符串恰好由 words 的所有单词拼接而成,每个单词仅对应地出现一次。
由于 words 里面单词是无序的,目标子字符串的成员单词也是无序的,所以,考虑用 Hashtable 作为数据存储。
需要充分利用的两个题目条件:
1. 所有单词等长
2. 目标子字符串是由单词连续无中断地连接而成
根据上面两个条件,可以将 s 全部分割为长度为 length 的子字符串,共有 length 种分法。length 种分法会覆盖全部需要找的子字符串。
对于每一种分割,可以将长度为 length 的子字符串视为不再分割的单元,利用滑动窗口算法(Slide Window Algorithm),线性时间找到符合条件的目标子字符串,O(n/length) 复杂度,其中 n 为 s 的长度。一共有 length 种分发,则耗时 O(length * n/length) = O(n)。
算法实现思路:
将 s 全部分割为长度为 length 的连续子串的方法,一共有 length 种。
对于每一种 k (0 <= k < length, 表示开始分割的起始下标 ) 有:
以 k 为起始位置,长度为 length 子字符串即为一个待验证子串 subs
左右指针指向首个 subs
若右指针指向的 subs 是要找的 word, 并且已记录的次数小于需要找到的次数,则记录新找到一个 subs,右指针右移
若右指针指向的 subs 是要找的 word,并且已记录的次数等于需要找到的次数, 则将左指针当前单词从记录中排除,并左指针右移,重复此操作直到从记录中排除一个右指针当前的 subs。使得 [ subs 已记录的次数小于需要找到的次数 ]。
若右指针指向的 subs 不是要找的 word,则右指针右移,左指针指向右指针位置,并情况记录。
若已找到的记录(左右指针内元素)恰好等于需要找到全部 words ,则保存左指针所在位置,即为一个需要找的下标。然后,将左指针当前元素从记录中排除,左指针右移。
实现代码:
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set; class Utility{ /**
* reset the value of matched result str_cnt_mch
*
* @param str_cnt_mch
*/
public static void resetHashtable(Hashtable<String, Integer> str_cnt_mch){
Set<Entry<String, Integer>> set = str_cnt_mch.entrySet();
for (Entry<String, Integer> s_c : set){
s_c.setValue(0);
}
}
} public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new LinkedList<Integer>(); Hashtable<String, Integer> str_cnt = new Hashtable<String, Integer>();
Hashtable<String, Integer> str_cnt_mch = new Hashtable<String, Integer>(); for (String wrd : words) {
if (str_cnt.containsKey(wrd)) {
str_cnt.put(wrd, str_cnt.get(wrd) + 1);
} else {
str_cnt.put(wrd, 1);
} str_cnt_mch.put(wrd, 0);
} int length = words[0].length(); for (int k = 0; k < length; k++) { int lft = k;
int rgh = k; Utility.resetHashtable(str_cnt_mch);
int mchCnt = 0; while (rgh + length <= s.length()) { String subs = s.substring(rgh, rgh + length); if (str_cnt.containsKey(subs)) { if (str_cnt_mch.get(subs) < str_cnt.get(subs)) { str_cnt_mch.put(subs, str_cnt_mch.get(subs) + 1);
mchCnt++; rgh += length; } else {
// the number of subs in str_cnt_mch is the same as that
// in str_cnt while (true) { String subsl = s.substring(lft, lft + length); str_cnt_mch.put(subsl, str_cnt_mch.get(subsl) - 1);
mchCnt--; lft +=length; if (subsl.equals(subs)) {
break;
}
}
}
} else {
// subs is not a word in words
Utility.resetHashtable(str_cnt_mch);
mchCnt = 0; rgh = rgh + length;
lft = rgh;
} if (mchCnt == words.length){
res.add(lft); String subsl = s.substring(lft, lft + length);
str_cnt_mch.put(subsl, str_cnt_mch.get(subsl) - 1);
mchCnt--; lft = lft + length;
}
}
} return res;
}
}
参考资料:
Substring with Concatenation of All Words -- LeetCode, Code_Ganker, CSDN
[LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java的更多相关文章
- 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 (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 串联所有单词的子串
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由所有单词连成的子串
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(确定包含所有子串的起始下标)
题目链接: 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 ...
随机推荐
- NSIndexPath初始化
在UITableView中经常用到这个类,但一直不知道怎么初始化,网上抄录的代码如下,果然好用 NSIndexPath *index = [NSIndexPath indexPathForRow:0 ...
- HDU 3037(Lucas定理)
对于很大的组合数不能用C(n, m) = C(n - 1, m) + C(n-1, m -1)来求,这里就用到Lucas定理. 模板题: hdu3037:模板如下: #include <cstd ...
- POJ 2007 Scrambled Polygon 凸包
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7214 Accepted: 3445 ...
- Python之路,Day13-----暂无正在更新中
Python之路,Day13-----暂无正在更新中
- 自由创造属于你的H5内容
在这里,你可以自由创造属于你的H5内容 mark下 http://www.ih5.cn/
- java_reflect_01
最近学习java开始接触到了框架,突然觉得java反射很重要,因此在这里做了一些总结(参考园中大苞米大神的文章) 首先我们要认识一下Class: 一.Class类有什么用? class类的实例表示ja ...
- [转]memmove函数
[FROM MSDN && 百科] 原型: void *memmove( void* dest, const void* src, size_tcount ); #include&l ...
- 利用js加载本地图片预览功能
直接上代码: 经测试,除safari6包括6以下不支持,其他均可正常显示. 原因:safari6不支持filereader,同时不能使用IE滤镜导致失效. fix: 可以利用canvas,解决safa ...
- Ecshop 数据库操作方法getRow、getAll、getOne区别
ECShop没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现.这样做的好处是实现非常轻量,大大减小了分发包的文件大小.另外,当网站需要做memcached缓存时,也可以 ...
- php 随机显示据今天30天内的任意一天
function randomDate() { //echo date( "Y-m-d H:m:s", $newtime); //echo date("Y-m-d H:m ...