题目描述:

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

解题思路:

首先设置一个HashMap,里面键值为单词,元素值为对应单词出现的次数,将其作为比较的模板。

我们首先在一个大的循环里面获得字符串的第一个位置开始的第一个单词,查看上述的模板Map中是否存在这个单词。如果不存在,那么外面的大循环直接后移一位,匹配下一个位置开始的单词。同时内嵌循环,用于逐个单词进行排查。如果查找是存在的,那么我们把这个单词加入到新的Map中,同时统计次数也需要递增。接下来查看这个新Map中的该单词出现的次数是否小于的个等于模板中该单词出现的次数,如果大于该次数,说明情况是不符合要求的,跳出该内层循环,外层循环的指针后移。

最后如果内层循环如果顺利走完,则说明从该位置开始所有的单词都是匹配的,那么将该位置添加到List中,否则外层循环指针指向下一个字符的外置,继续开始类似的判断。

代码如下:

public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
Map<String, Integer> map = new HashMap<String, Integer>();
Map<String, Integer> tmp = new HashMap<String, Integer>();
int sLength = s.length();
int wordsNum = words.length;
int wordsLength = words[0].length();
int j; if (sLength < wordsNum || wordsNum == 0)
return list;
for (int i = 0; i < wordsNum; i++) {
if (map.containsKey(words[i]))
map.put(words[i], map.get(words[i]) + 1);
else
map.put(words[i], 1);
}
for (int i = 0; i <= sLength - wordsNum * wordsLength; i++) {
tmp.clear();
for (j = 0; j < wordsNum; j++) {
String word = s.substring(i + j * wordsLength, i + j
* wordsLength + wordsLength);
if (!map.containsKey(word))
break;
if (tmp.containsKey(word))
tmp.put(word, tmp.get(word) + 1);
else
tmp.put(word, 1);
if (tmp.get(word) > map.get(word))
break;
}
if (j == wordsNum)
list.add(i);
}
return list;
}
}

Java [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. LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

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

  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 ☆☆☆

    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. xaml中绑定单例属性

    在项目中经常会遇到,同一个字典表绑定到多个ItemsControl上的情况,可以在单例中创建一个List,xaml上绑定即可.看代码: 1,XAML <Grid> <StackPan ...

  2. Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  3. C语言创建一个窗口提示

    打开Vs2012[我的是2012] /* X下面这些东西并没有什么用... 就不改了用2013 2015都一样 当然 devC++ 还有最原始的那个vc6.0也都是可以的. 编译环境遇到了相关问题网上 ...

  4. 【BZOJ 1009】 [HNOI2008]GT考试

    Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0< ...

  5. [SQL SERVER系列]读书笔记之SQL注入漏洞和SQL调优

    最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“'1'='1'”这个表达式永远返回 true, ...

  6. python学习笔记16(错误、异常)

    一.什么是错误,什么是异常 错误是指在执行代码过程中发生的事件,它中断或干扰代码的正常流程并创建异常对象.当错误中断流程时,该程序将尝试寻找异常处理程序(一段告诉程序如何对错误做出响应的代码),以帮助 ...

  7. Xcode 合并分支报错

    原理和操作步骤见如下转载的两篇文章, 我所使用的 svn 客户端软件是 Mac 下面的 Versions.app v1.06 这个版本包含一个多人开发的bug bug 的解决方案见我之前转载的两篇文章 ...

  8. Csharp volatile 关键字

    volatile 关键字指示一个字段可以由多个同时执行的线程修改.声明为 volatile 的字段不受编译器优化(假定由单个线程访问)的限制.这样可以确保该字段在任何时间呈现的都是最新的值. vola ...

  9. php站点

    thinkphp wordpress 记事狗 phpcms http://jingyan.baidu.com/article/4b07be3c61e93e48b380f3fd.html

  10. linq集合内部赋值

    linq集合内部赋值 比如将一个列的值,赋值给另一列 有三种方法: 1. e.Result.ToList().ForEach(n => n.IsIntermediarybool = SetIsI ...