在字符串s中找到包含字符串数组words所有子串连续组合的起始下标(words中的子串排列顺序前后不计但是需要相连在s中不能存在其他字符)
 
 
参考代码 :
 
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(确定包含所有子串的起始下标)的更多相关文章

  1. 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 ...

  2. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  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. TensorFlow-GPU:查看电脑显卡型号是否支持CUDN,以及相关软件下载与介绍

    1.显卡(GPU)是否支持CUDN https://developer.nvidia.com/cuda-gpus 2.了解基础知识 1)CUDA(Compute Unified Device Arch ...

  2. chrome浏览器表单自动填充默认样式(背景变黄)-autofill

    之所以出现这样的样式, 是因为Chrome会自动为input增加如下样式. 这个样式的优先级也比较高. 无法通过important覆盖(这就比较恶心了). 解决方案(3种): 1. 关闭自动保存账号密 ...

  3. js中的坑

    for in vs hasOwnProperty == === 对象比较用===,值比较用==. 严格运算用=== http://www.zhihu.com/question/31442029 著作权 ...

  4. Nginx安装 默认虚拟主机 Nginx用户认证 Nginx域名重定向

    Nginx安装 cd /usr/local/src (http://nginx.org/en/download.html) wget http://nginx.org/download/nginx-1 ...

  5. js设置随机切换背景图片

    <script> var imgs =["http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_2.jpg&q ...

  6. 国内各大安卓(Android)市场的上传方式、认领、通过审核有哪些不同,有什么值得注意的地方?

    6 个回答 赞同89反对,不会显示你的姓名 唐元鹏,扯淡爱好者 Jc droid.李明亮.知乎用户 等人赞同 作为一个android菜鸟开发者,代码水平不咋样,却练就了一身上传app的本领,大体说一下 ...

  7. lsof fuser

    使用fuser 或 lsof在一个挂载点中查找已打开的文件 fuser -mv /usr 查看有哪些进程在运行/usr中资源 sync fuser -km /media/usbdisk U盘无法卸载

  8. vue如何加搜狗联盟广告

    搜狗联盟广告和百度联盟广告大同小异,但是好在百度联盟广告的使用方法网上还是可以找到一些,而搜狗联盟广告的教程网上几乎找不到,都是最基本的创建代码位的教程. 在vue中如果要加入搜狗联盟广告,就不能直接 ...

  9. BarTender 2016表单中的“秤显示”控件

    BarTender 2016中的表单是一个非常实用的工具,它可以实现数据输出提示,查询提示和同一表单的记录选择.这些都离开可供添加的控件,“秤显示”控件也是我们打印尝尝需要涉及的,今天我们就来看看什么 ...

  10. [RN] 04 - React Navigation

    react-navigation和react-router的对比: 支持的平台: react-navigation: react-native react-router: react-native.r ...