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.

For example, given:
s"barfoothefoobarman"
words["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

解法1:

  题目要求在给定一个字符串s和一个字符串数组words(有m个字符串,长度均为n)的条件下,找出s中所有“由words中全部字符串按任意顺序串联而成”的子串。

  需要用到两个哈希表,第一个存储words数组,key为每一个word,value为出现的次数(考虑到数组中可能会有相同的字符串);第二个哈希表用于存储当前遍历到的子串,value为已出现次数。从s的第一个字符开始遍历,按顺序找到长度为n的子串part,判断part是否在第一个哈希表中,以及当前已出现次数是否超过words数组中的数量,不满足条件的跳出循环并从s的下一个字符继续寻找。

  时间复杂度为 O(n2)。

public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || words == null || words.length == 0) {
return res;
} HashMap<String, Integer> toFind = new HashMap<>();
for (String word : words) {
Integer k = toFind.containsKey(word) ? toFind.get(word) + 1 : 1;
toFind.put(word, k);
} int m = words.length, n = words[0].length();
HashMap<String, Integer> found = new HashMap<>();
for (int i = 0; i <= s.length() - m * n; i++) {
found.clear();
int j = i;
for (; j < i + m * n; j += n) {
String part = s.substring(j, j + n);
Integer a = toFind.get(part);
Integer b = found.get(part);
if (a == null || (b != null && a <= b)) {
break;
}
found.put(part, b == null ? 1 : ++b);
}
if (j == i + m * n) {
res.add(i);
}
} return res;
}
}

解法2:

  仔细研究上面的解法,会发现其中有很多多余的步骤。例如,s="abcdefghijklmn",n=3:第一次遍历从'a'开始,需要遍历"abc", "edf", "ghi"....;而第4次比较从'd'开始,又需要遍历"def", "ghi"....

  可以改变上面的思路,从一个字符一个字符遍历,变成一个单词一个单词遍历。将s按照word的长度n进行划分,如s长度为18,m=2,n=3,那么遍历的顺序为:第一次(0,3,6,9,12,15),第二次(1,4,7,10,13,16),第三次(2,5,8,11,14,17)。

  首先还是先将words中的单词存到哈希表toFind中。然后从0开始遍历,用left记录当前串联起来的子串的起始位置,curr表示当前的子串(长为n),哈希表found纪录目前串联子串中的每一个word情况。遍历时分以下几种情况:

  • curr在toFind中不存在,此时匹配中断,前面从left开始匹配到的都失效。因此,清空found,从下一个子串(curr+n)继续。
  • curr在toFind中存在,但在found中不存在,说明前面没有匹配到。因此,将curr记入found中,然后从下一个子串继续。
  • curr在toFind和found中都存在,并且found中的数量小于toFind中的数量。将found中的currz数量加一,然后从下一个子串继续。
  • curr在toFind和found中都存在,并且两者数量相同,此时再将curr记入found就超过数量限制了。因此,需要从left开始,找到最早出现的同curr相同的子串(设为dupl),并把dupl同之前的子串在found中的纪录删除,然后从left移到dupl的下一个子串,然后curr从curr的下一个子串继续。

  每次curr操作结束后,比较当前串联子串的长度是否与与words中所有word长度之和相等,相等则说明当前串联子串匹配成功,将left记入res中,然后从left的下一个子串继续进行。。。。

  当 i = 0 遍历完成时,清空found,然后从 i = 1 进行下一次循环。

  这个算法实现难度比较大,但是整体的时间复杂度为 O(n)。

public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || words == null || words.length == 0) {
return res;
} HashMap<String, Integer> toFind = new HashMap<>();
for (String word : words) {
Integer k = toFind.containsKey(word) ? toFind.get(word) + 1 : 1;
toFind.put(word, k);
} int m = words.length, n = words[0].length();
HashMap<String, Integer> found = new HashMap<>();
for (int first = 0; first < n; first++) {
found.clear();
int left = first; // 当前串联字符串的起始位置
for (int curr = first; curr < s.length() && left <= s.length() - m * n; curr += n) {
String part = s.substring(curr, curr + n);
if (!toFind.containsKey(part)) { // 如果part不存在,清空found并从下一个位置开始
found.clear();
left = curr + n;
continue;
} if (!found.containsKey(part)) {
found.put(part, 1);
} else if (found.get(part) < toFind.get(part)) {
found.put(part, found.get(part) + 1);
} else {
// found和toFind中part数量相同,再添加就超出了,因此需要从left开始,
// 找到第一个part相同的子串,将其与之前的子串删去,再将其下一个字符串为起点。
while (!part.equals(s.substring(left, left + n))) {
Integer x = found.get(s.substring(left, left + n));
found.put(s.substring(left, left + n), x - 1);
left += n;
}
// 此处需要从found中删除left,再添加curr,但两者相同,故可抵消
left += n;
} // 如果当前串联子串的长度与数组长度相同,说明成功匹配了一个,
// 将其记录到res后,从left的下一个子串再继续
if (curr - left == (m - 1) * n) {
res.add(left);
Integer y = found.get(s.substring(left, left + n));
found.put(s.substring(left, left + n), y - 1);
left += n;
}
}
} return res;
}
}

[LeetCode] 30. Substring with Concatenation of All Words ☆☆☆的更多相关文章

  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 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

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

  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. 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由所有单词连成的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

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

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

  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. 欢迎来怼——第四次Scrum会议

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/16 17:15~17:40,总计25min.地点:东北师范 ...

  2. 软件工程 speedsnail 第二次冲刺10

    20150527 完成任务:蜗牛碰到线后速度方向的调整:已经基本实现多方向的反射: 遇到问题: 问题1 反射角的问题 解决1 利用tan()三角函数 明日任务: 大总结.找到新问题.布置下一次冲刺方案

  3. Communications link failure--分析之(JDBC的多种超时情况)

    本文是针对特定的情景下的特定错误,不是所有Communications link failure错误都是这个引起的,重要的区分特点是:程序是不是在卡主后两个小时(服务器的设置)后程序才感知到,才抛出了 ...

  4. RAID卡服务器安装2003教程

     这里先讲讲安装系统的几个思路: 1.U盘安装法(U盘只做可启动PE,常用的大白菜,IT天空,老毛桃.....拷贝系统ISO镜像到U盘,进入PE之后找到ISO,用虚拟光驱加载,运行WIN系统安装器 ...

  5. 【PHP】session失效时间

    最近用到php中session时,忽然发现php中的session有点让人头疼啊,要设置一个严格的特定时间内过期的session还真不太容易!后来在网上查询时,发现这个问题还真是有点普遍,网上也有关于 ...

  6. 10个linux网络和监控命令

    我下面列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nsloo ...

  7. 平衡树以及AVL树

    平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...

  8. HDU4681_String

    这个题目是这样的. 给你三个字符串A,B,C,(C一定是A和B的一个公共子序列). 现在要求你构造出一个串D,使得D同时为A和B的子序列,且C是D的一个连续子串.求D的最大可能长度. 很简单的一个DP ...

  9. 【bzoj2351】[BeiJing2011]Matrix 二维Hash

    题目描述 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过.所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...

  10. selenium学习网址

    1.http://www.testclass.net/selenium_java/#      testclass网址 2.http://www.yiibai.com/selenium/seleniu ...