LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
2017-03-10 17:22
417 查看
题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description
在字符串s中找到包含字符串数组words所有子串连续组合的起始下标(words中的子串排列顺序前后不计但是需要相连在s中不能存在其他字符)
参考代码 :
在字符串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 串联所有单词的子串
- leetCode 30.Substring with Concatenation of All Words (words中所有子串相连) 解题思路和方法
- LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)
- Substring with Concatenation of All Words字符串中找到包含所有单词的子串
- LeetCode----30. Substring with Concatenation of All Words(串联所有的words)
- LeetCode 30. Substring with Concatenation of All Words(所有单词的连接)
- [理解leetcode解法]30. Substring with Concatenation of All Words找子串
- leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法
- LeetCode | Substring with Concatenation of All Words(链接所有单词的子串)
- (Java) LeetCode 30. Substring with Concatenation of All Words —— 与所有单词相关联的字串
- [Leetcode] Substring with concatenation of all words 串联所有单词的子串
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联所有单词的子串)】
- LeetCode--Substring with Concatenation of All Words 串联所有单词的子串
- [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串
- leetcode 30 Substring with Concatenation of All Words
- leetcode 30 -- Substring with Concatenation of All Words
- LeetCode 30 - Substring with Concatenation of All Words
- LeetCode(30) Substring with Concatenation of All Words
- LeetCode 30.Substring with Concatenation of All Words
- LeetCode(30) Substring with Concatenation of All Words