题目描述:

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. H264相关代码

    H.264格式的视频打包成RTP后进行发送,编译环境为VC6++ #include <stdio.h> #include <stdlib.h> #include <str ...

  2. WPF处理Windows消息

    WPF中处理消息首先要获取窗口句柄,创建HwndSource对象 通过HwndSource对象添加消息处理回调函数. HwndSource类: 实现其自己的窗口过程. 创建窗口之后使用 AddHook ...

  3. 1035 Password (20)

    #include <stdio.h> #include <string.h> struct MyStruct { ]; ]; bool changed; }; int main ...

  4. 2016022601 - redis入门了解

    今天开始学习redis,先从网页上学习,主要学习地址是:易百中的redis和redis中国网站. 此片章学习来自于自:http://www.yiibai.com/redis/redis_quick_g ...

  5. 2016 系统设计第一期 (档案一)MVC 和 Bootstrap 表单转换

    bootstrap <form role="form"> <div class="form-group"> <label for= ...

  6. cocos2dx之Lua调用C++

    现在cocos2dx3.8自己封装了以前的toLua++,比以前更好用了. 先来看一下整体步骤: 1.编写一个.ini文件. 2,修改genbindings.py脚本. 3,执行genbindings ...

  7. angular service/directive

    <html class=" js cssanimations csstransitions" ng-app="phonecatApp" > < ...

  8. 深入浅出百度地图API开发系列(2):创建地图

    上一篇文章里,先介绍了一下百度地图API开发所涉及到的一些基础概念,包括投影,坐标系等基础概念,再有了这些基础后,我们可以开始开发自己的web地图了.先来个代码示例(建议大家都是用百度地图API大众版 ...

  9. 团体程序设计天梯赛-练习集L1-023. 输出GPLT

    L1-023. 输出GPLT 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个长度不超过10000的.仅由英文字母构成的 ...

  10. 免费素材:25套免费的 Web UI 设计的界面元素(转)

    Web 元素是任何网站相关项目都需要的,质量和良好设计的元素对于设计师来说就像宝贝一样.如果您正在为您的网站,博客,Web 应用程序或移动应用程序寻找完美设计的网页元素,那么下面这个列表会是你需要的. ...