LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description

在字符串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;
    }
}
时间: 2024-10-25 21:24:44

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, 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 an

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, 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 an

[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 starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters. For example, give

[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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. Example 1: Input

[*leetcode 30] Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "b

19.1.30 [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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. Example 1: Input

LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)

翻译 给定一个字符串S,一个单词的列表words,全是相同的长度. 找到的子串(多个)以s即每个词的字串联恰好一次并没有任何插入的字符所有的起始索引. 原文 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

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 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 exampl

LeetCode 30 Substring with Concatenation of All Words (C,C++,Java,Python)

Problem: 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 wordsexactly once and without any intervening characters. For exam