在字符串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. HTML5 3D爱心动画及其制作过程

    之前有看到过很多基于HTML5或者CSS3制作的爱心动画,不过基本上都是2D平面的,今天在国外的网站上看到一个基于HTML5 3D的爱心动画,对于HTML5爱好者来说,不免兴奋了一把.下面将分享一下这 ...

  2. 格林威治时间(GTM)转北京时间

    需要模块:pytz(三方)和datetime import pytz import datetime time = "Tue, 12 Jun 2012 14:03:10 GMT" ...

  3. Python自然语言处理学习——jieba分词

    jieba——“结巴”中文分词是sunjunyi开发的一款Python中文分词组件,可以在Github上查看jieba项目. 要使用jieba中文分词,首先需要安装jieba中文分词,作者给出了如下的 ...

  4. 2. 自动化运维系列之Cobbler给Openstack节点安装操作系统。

    preface 我们在一篇博文知道了如何搭建Cobbler,那么下面就通过Cobbler来安抓Openstack所有节点吧. 服务器配置信息如下: 主机名 IP 角色 Cobbler.node.com ...

  5. WebGL 着色器语言(GLSL ES)

    1.类型转换内置函数 转换/函数/描述 转换为整形数/int(float)/将浮点数的小数部分删去,转换为整形数(比如,将3.14转换为3) 转换为整形数/intl(bool)/true被转换为1,f ...

  6. CStringArray error C2248: 'CObject::CObject' : cannot access private member declared in class

    在开发中将一个字符串分割,并将子字符串保存在CStringArray中,专门写了一个函数,如下: SplitStringToCString(CString str, TCHAR tszSplit, C ...

  7. phpcms 字符截取str_cut的使用

    PHPCMS中截取字符串用的是 str_cut 系统函数,通常在输出标题或者是内容摘要的时候使用来限制字符串的字符,这样就可以防止因字符串而变成的页面变形等问题. 我们来看一下这个函数,在PHPCMS ...

  8. 如何在 CentOS 7 上安装 Docker

    Docker 是一个开源工具,它可以让创建和管理 Linux 容器变得简单.容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.Docker 帮助系统管理员和程序员在容器中开发应用程序,并且 ...

  9. Git------如何使用Git Bash Here提交代码

    转载:码云帮助文档地址 http://git.mydoc.io/?t=154712 1.打开“Git Bash Here” 2.输入: ssh-keygen -t rsa -C "xxxxx ...

  10. Go之类型判断

    boy := util.Boy{util.Person{"Eric", 19, "boy"}, "1"} var boyClone inte ...