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

解题思路:

由于涉及到对words的查询

因此第一步:将words[] put进图里,key代表单词,value代表单词次数。

考虑到s很长,一步一步走的话其实有很多时候可以利用之前的结果,因此可以以word[0].length为单位进行一次遍历(类似于奇偶),这样就可以使用前面的结果了。

然后设一个指针,每次移动word[0].length步,检查是否在words的图里面,如果有的话,也把它放到另外一张图里面,并引用一个计数器,如果计数器长度和words[].length的长度一致的话,那么找到一组解,指针后移。当然,里面有很多判断条件,具体见代码,JAVA实现:

	static public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
if (s.length() == 0 || words.length == 0 || words[0].length() == 0)
return list;
HashMap<String, Integer> wordsMap = new HashMap<String, Integer>();
for (String string : words) {
if (!wordsMap.containsKey(string))
wordsMap.put(string, 1);
else
wordsMap.put(string, wordsMap.get(string) + 1);
}
for (int i = 0; i < words[0].length(); i++) {
int StartIndex = i, wordsLength = 0;
HashMap<String, Integer> tmap = new HashMap<String, Integer>();
for (int j = i; j <= s.length() - words[0].length(); j += words[0].length()) {
String str = s.substring(j, j + words[0].length());
if (wordsMap.containsKey(str)) {
if (tmap.containsKey(str))
tmap.put(str, tmap.get(str) + 1);
else
tmap.put(str, 1);
wordsLength++;
while (tmap.get(str) > wordsMap.get(str)) {
String startWord = s.substring(StartIndex,StartIndex + words[0].length());
StartIndex += words[0].length();
tmap.put(startWord, tmap.get(startWord) - 1);
wordsLength--;
}
if (wordsLength == words.length) {
list.add(StartIndex);
String startWord = s.substring(StartIndex,StartIndex + words[0].length());
tmap.put(startWord, tmap.get(startWord) - 1);
wordsLength--;
StartIndex += words[0].length();
}
}
else {
tmap.clear();
wordsLength = 0;
StartIndex = j + words[0].length();
}
}
}
return list;
}

Java for LeetCode 030 Substring with Concatenation of All Words【HARD】的更多相关文章

  1. LeetCode 030 Substring with Concatenation of All Words

    题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...

  2. LeetCode:二叉搜索树中的搜索【700】

    LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...

  3. LeetCode:N叉树的后序遍历【590】

    LeetCode:N叉树的后序遍历[590] 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 题目分析 这道题有好几 ...

  4. LeetCode:下一个更大元素I【31】

    LeetCode:下一个更大元素I[31] 题目描述 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的 ...

  5. LeetCode:二叉树的锯齿形层次遍历【103】

    LeetCode:二叉树的锯齿形层次遍历[103] 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 ...

  6. LeetCode:反转字符串中的元音字母【345】

    LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...

  7. LeetCode:删除排序数组中的重复项||【80】

    LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

  8. LeetCode:安排工作以达到最大收益【455】

    LeetCode:安排工作以达到最大收益[455] 题目描述 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[ ...

  9. LeetCode:用最少的箭引爆气球【452】

    LeetCode:用最少的箭引爆气球[452] 题目描述 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道 ...

随机推荐

  1. 最大ASCII的和问题

    问题:One day when you are going to clear all your browsing history, you come up with an idea: You want ...

  2. bzoj 3172 后缀数组|AC自动机

    后缀数组或者AC自动机都可以,模板题. /************************************************************** Problem: 3172 Us ...

  3. jquery中的prop和attr比较区别

    近期和一同事争执prop和attr的区别,也查了很多,同事说它只是特性和固有属性的区别,但是我也查到了一些其他的,故此,来总结一下吧! 1.固有属性和特别属性 对于HTML元素本身就带有的固有属性,在 ...

  4. jQuery根据下拉列表的选择进行不同的操作

    需求:选择了某个下拉列表选项,进行不同的操作 代码部分: <!doctype html> <html> <head> <meta charset=" ...

  5. ios 图片尺寸

  6. Unity Shader Billboard

    记录来源于ShaderLab开发实战详解 Shader "Tut/Project/Billboard_1" { Properties { _MainTex ("Base ...

  7. memcache的内存回收机制

    memcache不会释放内存,而是重新利用. 在缓存的清除方面,memcache是不释放已分配内存.当已分配的内存所在的记录失效后,这段以往的内存空间,memcache只会重复利用. memcache ...

  8. iptables 工具

    iptables 工具 参考文档: https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html   ...

  9. SQL注入小结

    分类学习有利于条理化知识,大致的SQL注入分为三种: 1.BealeanBase 2.TimeBase 3.ErrorBase 1.从最简单的说起,基于布尔类型是最常见的SQL注入方式 select ...

  10. 把Linux安装到移动硬盘上

    把Linux安装到移动硬盘上 转载于:http://mrkh.me/install-linux-on-a-portable-hard-drive.html 这一篇文章讲一下,怎么把linux安装到移动 ...