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 ...
随机推荐
- 怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表? --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...
- 使用composer安装项目依赖
Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们 下载并安装composer composer官方文档有好几种安装方法,此处只介绍我实验过 ...
- VS.net中快捷键收缩和展开代码段 (转)
i. Ctrl-M-O 折叠所有方法 ii. Ctrl-M-P 展开所有方法并停止大纲显示(不可以再折叠了) iii. Ctrl-M-M 折叠或展开当前方法 iv. Ctrl-M-L展开所 ...
- 关于linq to sql类线程同步问题
例如,下面一段代码,当两个线程同时访问时会出现各种由于线程不同步而导致的问题,比如什么DataReader已打开未关闭啊,已经添加了重复的键啊等等. /// <summary> /// 当 ...
- PCB Layout高速电路设计小知识点
1.单位:1mil = 0.0254mm 2.default线宽及线距(综合考虑高速电路性能及PCB板厂制程能力):6mil 3.差分走线特征阻抗:100ohm 4.3W原则,即线间距采用3倍线宽,多 ...
- SAP IDOC开发(转)
创建IDOC: 第一步:WE31 创建IDOC所包含的字段. 第二步:WE30 创建IDOC 把Segment分配给IDOC第三步:WE81 创建信息类型第四步:WE82 把IDOC类型与信息 ...
- Java Swing 第02记 标签和按钮
JLable的常用构造器 Public JLabel() 创建无图像且其标题为空字符串的JLabel对象 Public JLabel(String text) 使用指定的字符串text(也就是标签显示 ...
- JSP自定义标签——调用数据库(通过id号搜索相关信息)
一.创建新表(假设在master数据库下新建) 二.连接数据库 开始-->控制面板-->管理工具-->数据源-->系统DSN-->添加-->SQL Server-- ...
- HTML5日期输入类型(date)
在很多页面和web应用中都有输入日期和时间的地方,最典型的是订飞机票,火车票,酒店,批萨等网站. 在HTML5之前,对于这样的页面需求,最常见的方案是用Javascript日期选择组件.这几乎是无可争 ...
- javascript常用数组算法总结
1.数组去重 方法1: JavaScript //利用数组的indexOf方法 function unique (arr) { var result = []; for (var i = 0; i & ...