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 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的更多相关文章
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [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 ...
- 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 ...
- [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 ...
- LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description 在字符 ...
- [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 ...
- [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 ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- 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 ...
随机推荐
- NYOJ 994 海盗分金 逆向递推
链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=994 题意: 有n个海盗劫得了窖藏的m块金子,并准备瓜分这些战利品.按照古老流传下来的分金法则 ...
- jQuery 点击按钮刷新页面
//页面加载时绑定按钮点击事件 $(function () { $("#按钮id").click(function () { refresh(); }); }); //点击按钮调用 ...
- doc下批处理文件的感想
这段时间忙着为我们的爬虫程序做一个守护进程,想来想去还是用脚本比较好,所以用了点时间仔细的研究了一下,这里有一点点经验想分享给大家,也不能说是经验了,只能说是我写这个的时候所用到的知识: 1.task ...
- 友盟分享在appdelegate中的调用语句举例
//友盟 [UMSocialData setAppKey:UmengAppkey]; [UMSocialConfig setSupportedInterfaceOrientations:UIInter ...
- Android Studio 单刷《第一行代码》系列 06 —— Fragment 生命周期
前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...
- 3150 Pibonacci数 - Wikioi
题目描述 Description 你可能听说过的Fibonacci数和圆周率Pi. 如果你让这两个概念合并,一个新的深奥的概念应运而生:Pibonacci数. 这些数可以被定义为对于x>=0: ...
- linux复制多个文件到文件夹
linux复制多个文件到文件夹 cp file1 file2 file3 directory即将文件file1 file2 file3复制到directory
- spoj 178
输出相邻的点 比较简单吧....... #include <cstdio> #include <cstring> using namespace std; int main ...
- dll的概念 dll导出变量 函数 类
1. DLL的概念 DLL(Dynamic Linkable Library),动态链接库,可以向程序提供一些函数.变量或类.这些可以直接拿来使用. 静态链接库与动态链接库的区别: (1)静态链接 ...
- HDU1569+最大点权集
/* 最大点权独立集=总权值-最小点权覆盖集 最大点权独立集=最大流 最小点权覆盖集=最小割 题意: 给你一个m*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格 ...