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的更多相关文章

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

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

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

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

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

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

  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. C#“简单加密文本器”的实现

    本示例只能加密英文文本,使用的算法为异或算法.源代码:http://pan.baidu.com/share/link?shareid=3241348313&uk=1761850335(本示例属 ...

  2. 12、SQL Server 行列转换

    SQL Server 行转列 在SQL Server 2005中PIVOT 用于将列值转换为列名(行转列),在SQL Server 2000中是没有这个关键字的 只能用case语句实现. --创建测试 ...

  3. Phonegap 安卓的自动升级与更新。当版本为4.0的时候

    清单文件中: <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/> ...

  4. Android打开系统的Document文档图片选择

    打开Document UI 过滤图片 private void startAcitivty() { Intent intent = new Intent(); intent.setAction(&qu ...

  5. ASP.NET菜鸟之路之Request小例子

    背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Request获取值 Request获取值有两种 ...

  6. Ajax的工作流程简述

    提到Ajax相信我们都不会陌生,不管你是前端开发还是后台数据处理的程序员,ajax的作用就像现在生活中的手机一样,无论是作用还是流程都差不多,这里我们要进行ajax操作后台数据并显示在页面上的话,首先 ...

  7. POJ 2395 Out of Hay(最小生成树中的最大长度)

    POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时, ...

  8. 巧用Session Manager还原Firefox丢失会话

    今天Firefox Crash之后,我的会话全部丢失了.按照以往来说,Firefox在重新启动之后或者Crash之后会有一个会话还原的页面.但今天确实没有.后来我进行Google查阅,试了很多种办法. ...

  9. Thinkphp发布文章获取第一张图片为缩略图实现方法

    正则匹配图片地址获取第一张图片地址 此为函数 在模块或是全局Common文件夹中的function.php中 /** * [getPic description] * 获取文本中首张图片地址 * @p ...

  10. php解析json数据

    <?php $data; $data.="["; for ($i=0;$i<20;$i++) { $data.="{"; $data.=" ...