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).
简单的翻译:给定一个字符串s和一个字列表words,所有的字是相同长度的。找出所有s的字串中包含有words中所有元素的下标,并且顺序无所谓。
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}
对于每一次的移动判断,判断窗体内的字符串是否是有给定的字符串数组中的元素组成,这个是用map或者其他的数据结构来判断的。因为是不用计较顺序的,所以有可以简化为判断存在与否的问题。判断存在与否则是通过累计判断的,即每次从输入串提取出wordsp[0].length长度的字串判断其是否存在于words中,并且数目一定要相同,即words中有一个“abaad”,则窗口对应的串中也只能有一个”abaad”。
使用一个map保存words中的数据用来对比,一个新的map用来保存窗对应的数据,这样通过比较两个map中的数据就可以判断是否匹配了。并且使用map结构可以减少取值的操作过程,当每次窗移动时,只需要从map中除去原窗口位置对应的第一个word即可,这样新的串口对应的数据只需要添加一个word即可,可以减少words.length-1次的提取数据的操作。(代码来自网上分享)
public class Solution { /*
A time & space O(n) solution
Run a moving window for wordLen times.
Each time we keep a window of size windowLen (= wordLen * numWord), each step length is wordLen.
So each scan takes O(sLen / wordLen), totally takes O(sLen / wordLen * wordLen) = O(sLen) time. One trick here is use count to record the number of exceeded occurrences of word in current window
*/
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if(words == null || words.length == 0 || s.length() == 0) return res;
int wordLen = words[0].length();
int numWord = words.length;
int windowLen = wordLen * numWord;
int sLen = s.length();
HashMap<String, Integer> map = new HashMap<>();
for(String word : words) map.put(word, map.getOrDefault(word, 0) + 1); for(int i = 0; i < wordLen; i++) { // Run wordLen scans
HashMap<String, Integer> curMap = new HashMap<>();
for(int j = i, count = 0, start = i; j + wordLen <= sLen; j += wordLen) { // Move window in step of wordLen
// count: number of exceeded occurences in current window
// start: start index of current window of size windowLen
if(start + windowLen > sLen) break;
String word = s.substring(j, j + wordLen);
if(!map.containsKey(word)) {
curMap.clear();
count = 0;
start = j + wordLen;
}
else {
if(j == start + windowLen) { // Remove previous word of current window
String preWord = s.substring(start, start + wordLen);
start += wordLen;
int val = curMap.get(preWord);
if(val == 1) curMap.remove(preWord);
else curMap.put(preWord, val - 1);
if(val - 1 >= map.get(preWord)) count--; // Reduce count of exceeded word
}
// Add new word
curMap.put(word, curMap.getOrDefault(word, 0) + 1);
if(curMap.get(word) > map.get(word)) count++; // More than expected, increase count
// Check if current window valid
if(count == 0 && start + windowLen == j + wordLen) {
res.add(start);
}
}
}
}
return res;
}
}
关于外层循环的存在,从我们的绘图可以看到窗的出发点是0,但是有可能从1开始的窗对应的才是我们想要的,所以要加入外层循环遍历所有的可能,之所以到words[0].length就结束是因为,当窗的开始为值为words[0].length的时候,可以发现它是第一次移动窗的结果,也就是重复了,所以就不用继续执行了。想象一下即可。
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
Substring with Concatenation of All Words的更多相关文章
- 【leetcode】Substring with Concatenation of All Words
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- leetcode面试准备: Substring with Concatenation of All Words
leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...
- [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][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- leetcode-algorithms-30 Substring with Concatenation of All Words
leetcode-algorithms-30 Substring with Concatenation of All Words You are given a string, s, and a li ...
- LeetCode: Substring with Concatenation of All Words 解题报告
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- 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 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 ...
- 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 ...
随机推荐
- Android两个页面之间的切换效果工具类
import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; ...
- 作业七:团队项目——Alpha版本冲刺阶段007
今日进展:完善游戏主体代码. 今日安排:让游戏能运行起来.
- sql case when then else end
我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' else '女' end) AS sex from st ...
- 无约束优化算法——牛顿法与拟牛顿法(DFP,BFGS,LBFGS)
简介:最近在看逻辑回归算法,在算法构建模型的过程中需要对参数进行求解,采用的方法有梯度下降法和无约束项优化算法.之前对无约束项优化算法并不是很了解,于是在学习逻辑回归之前,先对无约束项优化算法中经典的 ...
- 使用Windows Form 制作一个简易资源管理器
自制一个简易资源管理器----TreeView控件 第一步.新建project,进行基本设置:(Set as StartUp Project:View/Toolbox/TreeView) 第二步.开始 ...
- 【zz】matlab 求差集
matlab判断2个数组中不同元素--setdiff c = setdiff(A, B) 返回在A中有,而B中没有的值,结果向量将以升序排序返回.在集合论中,c = A - B.A和B也可以是字符串细 ...
- C语言程序设计第6堂作业
一. 本次课主要内容: 通过几个实例来进一步掌握循环结构的应用. 二. 实验内容:(70分) 1. 穷举算法(古典算术问题-搬砖头)某地需要搬运砖块,已知 ...
- jsp提交表单数据乱码,内置对象,以及过滤器
jsp提交表单数据乱码解决方案 通过form表单给服务器提交数据的时候,如果提交的是中文数据,那么可能会出现乱码,如果表单的请求方式是post请求,那么可以使用如下方案解决乱码: 在调用getPara ...
- [Python Fabric] [SSH] Mac OS X 10.9 + Vagrant虚拟环境使用Python Fabric进行SSH远程登录的简单实验
1. ssh客户端生成key $ Generating public/private rsa key pair. Enter file in which to save the key (/Users ...
- 第9章 硬件抽象层:HAL
HAL(硬件抽象层)是建立在Linux驱动之上的一套程序库.这套程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.在传统的Linux系统中Linux驱动一般有两种类型的代码:访问硬件 ...