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. 【POJ 2250】Compromise(最长公共子序列LCS)

    题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...

  2. TCP/IP详解 笔记一

    概述: Tcp-ip让网络上的计算机进行通信,而不管计算机和操作系统是否一样. 分层结构: Tcp/ip协议族是多层协议的组合,而tcp和ip只是其中的两个协议而已. 一个通信举例: 注意图的右上方: ...

  3. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  4. 深入解析MySQL分区(Partition)功能

    自5.1开始对分区(Partition)有支持 = 水平分区(根据列属性按行分)= 举个简单例子:一个包含十年发票记录的表可以被分区为十个不同的分区,每个分区包含的是其中一年的记录. === 水平分区 ...

  5. c# 闭包 小例

    class Program { static void fnnn() { string[] k = new string[] { "x", "y", " ...

  6. ci创建zip

    public function createZip() { $this->load->library("zip"); $name = "test.text&q ...

  7. 一机双mysql的安装和启动注意事项目

    ./configure --prefix=/usr/local/mysql5.1/ --with-mysqld-user=mysql --sysconfdir=/usr/local/mysql5.1/ ...

  8. ctags 的最简单使用

    vim插件ctags的安装和使用 2013-11-19 20:47 17109人阅读 评论(0) 收藏 举报  分类: 开发工具(3)  linux编程(9)  c/c++编程(11)  版权声明:本 ...

  9. Linux(Ubuntu)下面SecureCRT 完全破解

    转载声明:本文来自http://www.boll.me/archives/680 相关说明: 上篇发了个Linux(Ubuntu) 下 SecureCRT 7 30天循环破解在启动的时候会多输入一次确 ...

  10. ldconfig deferred processing now taking place

    在ubuntu下面安装软件,安装结束后,提示:ldconfig deferred processing now taking place 到网上查询了一下,大概意思是说:软件安装完了,是否要重启电脑.