在字符串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. springboot - Constructor、@Autowired、@PostConstruct分析

    1.Constructor 构造方法 2.@Autowired 依赖注入 3.@PostConstruct 在依赖注入完成后被自动调用 4. 三者的顺序: 从依赖注入的字面意思就可以知道,要将对象p注 ...

  2. Linux下MySQL开放root的远程访问权限

    mysql默认只能从本地连接,所以要使root可以远程访问登录,需做如下设置: 1.授权 请使用以下命令 mysql> Grant all privileges on *.* to 'root' ...

  3. Fragment管理工具类

    Fragment相关→FragmentUtils.java→Demo addFragment : 新增fragment removeFragment : 移除fragment replaceFragm ...

  4. Lua中用Split函数分割字符串

    function Split(szFullString, szSeparator) local nFindStartIndex = local nSplitIndex = local nSplitAr ...

  5. Android App Crash, send email to author

    http://www.iriphon.com/2011/06/23/how-do-i-get-android-crash-logs/

  6. c# 开发window服务

    http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html 安装 cmd 输入 InstallUtil.exe E:\TestApp\ ...

  7. maven的多环境配置

    <profiles> <profile> <id>dev</id> <activation> <activeByDefault> ...

  8. linux ss命令使用详解

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...

  9. 存储过程中SELECT INTO的使用

    在MySQL存储过程中使用SELECT …INTO语句为变量赋值: 用来将查询返回的一行的各个列值保存到局部变量中. 要求: 查询的结果集中只能有1行. SELECT col_name[,...] I ...

  10. vs2010,vs2012如何连接vss2005,vss2008

    打开vs2010.依次打开[工具]-[选项]-[源代码管理] 这个时候可以看到管理插件中有Microsoft Visual SourceSafe选项(若没有该选项,重新安装VSS即可). 连接上项目后 ...