LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
package leetcode_50; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /***
*
* @author pengfei_zheng
* 匹配words所有元素
*/ public class Solution30 {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<Integer>();
int wordLength = words[0].length(), patternLength = wordLength * words.length;
if (patternLength > s.length()) {
return result;
}
// array[0] stores the word count in the given pattern
// array[1] stores the word count in the actual string
int[][] wordCountArr = new int[2][words.length]; // This map is used to maintain the index of the above array
Map<String, Integer> wordCountIndexMap = new HashMap<String, Integer>(); // storing the word counts in the given patter. array[0] is populated
for (int i = 0, idx = 0; i < words.length; i++) {
if (wordCountIndexMap.containsKey(words[i])) {
wordCountArr[0][wordCountIndexMap.get(words[i])]++;
} else {
wordCountIndexMap.put(words[i], idx);
wordCountArr[0][idx++]++;
}
} // this is required to cover use case when the given string first letter
// doesnt corresponds to any matching word.
for (int linearScan = 0; linearScan < wordLength; linearScan++) {
int left = linearScan, right = linearScan, last = s.length() - wordLength, wordMatchCount = words.length; // reset word counts for the given string
Arrays.fill(wordCountArr[1], 0); // this logic same as minimum window problem
while (right <= last) {
while (wordMatchCount > 0 && right <= last) {
String subStr = s.substring(right, right + wordLength);
if (wordCountIndexMap.containsKey(subStr)) {
int idx = wordCountIndexMap.get(subStr);
wordCountArr[1][idx]++;
if (wordCountArr[0][idx] >= wordCountArr[1][idx]) {
wordMatchCount--;
}
} right += wordLength;
} while (wordMatchCount == 0 && left < right) {
String subStr = s.substring(left, left + wordLength);
if (wordCountIndexMap.containsKey(subStr)) {
// this check is done to make sure the sub string has
// only the given words.
if ((right - left) == patternLength) {
result.add(left);
} int idx = wordCountIndexMap.get(subStr);
// if this condition is satisfied, that means now we
// need to find the removed word in the remaining string
if (--wordCountArr[1][idx] < wordCountArr[0][idx]) {
wordMatchCount++;
}
} left += wordLength;
}
}
} return result;
}
}
LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)的更多相关文章
- 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
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [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 解题思路 - 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 ☆☆☆
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- 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 a ...
- [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 ...
随机推荐
- php json_decode无法解析特殊问好字符
在通过别人接口请求信息的时候,偶尔会遇到由于部分字符,如以下情况,则通过json_decode是会返回null的 但是这种情况通常不是由于整体编码的问题,因为在解析的时候就是以utf-8的编码解析的 ...
- fedora26 编译内核出现Can't use 'defined(@array)' 错误
cd /kernel/ vim timeconst.pl 把373行中的if (!defined(@val)) { 改为if (!@val) {
- 腾讯QQ积分CSRF导致积分任意挥霍(我的积分为什么少了)
触发点:http://jifen.qq.com/html5/index.html?ADTAG=JIFEN.MART.INDEX 随意兑换一个商品: 因为刚才我已经兑换过了,所以积分没有了.. 没关系, ...
- 加密算法(扩展知识:Base64编码)
在某些考虑数据安全的场景下,我们常常会用到加密解密.编码解码知识.比如把用户密码保存到数据库上,常用的方式是通过MD5或SHA1不可逆算法进行加密后密文保存. 这里主要介绍三种常用的加密算法: (1) ...
- 点击除元素以外的任意地方隐藏元素js
比如想实现点击列表弹出筛选器,点击其他任意地方关闭筛选器,如图 该筛选器class名 $(document).click(function () { $(".subMenu").h ...
- windows下caffe安装配置、matlab接口
一.CommonSettings.props caffe下载后解压.源代码文件夹caffe-master,到该文件夹下的windows文件夹下,将CommonSettings.props.exampl ...
- 深入理解css3中的flex-grow、flex-shrink、flex-basis
https://www.cnblogs.com/ghfjj/p/6529733.html
- Get started with ros -- 1
原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ 一.Introduction: 机器人操作系统(ROS)是使机器人系统的不同部分能够发 ...
- 关于Android打版本号的小白文
尽管常常和android打交道.但事实上我对android不是非常了解. 这里记录一下ant编译androidproject的过程,然后顺便记录一下android的一些基本概念.不求渡人,但求渡己.这 ...
- Linux下tomcat无法启动
场景:干净的tomcat,刚解压 1 通过./startup.sh,提示启动成功,但查看没有日志 2 通过netstat -tln查看端口,发现找不到8080 3 通过./catalina.sh ru ...