leetcode 之 Substring with Concatenation of All Words

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: "barfoothefoobarman"

L: ["foo", "bar"]

You should return the indices: [0,9].

(order does not matter).

思路:题目的意思是找到包含L中所有单词的起始位置,但是不能多,也不能少,要刚好包含L中的全部单词,而且相邻的两个开始位置还可以重叠,比如:S="aaa",L=[‘a‘,‘a‘],结果是[0,1]。具体方法和最短摘要有点类似,用need数组保存需要的所有单词,hasFind保存已经找到的单词,如果hasFind大于need,则该位置不满足,只有当两个数组完全相同时才满足。

class Solution {
public:
    vector<int> findSubstring(string S, vector<string> &L)
    {
    	int totalWord = L.size(),wordLen = L[0].size(),i,j;
    	map<string,int> needWord;
    	vector<int> res;
    	for (i = 0;i < totalWord;i++)needWord[L[i]]++;//统计需要的单词
    	for(i = 0;i + totalWord*wordLen <= S.size();i++)
    	{
    		map<string,int> hasFindWord;
    		for (j =0;j < totalWord;j++)//对于每一个单词
    		{
    			string sub = S.substr(i+j*wordLen,wordLen);//提取每一个可能的单词
    			map<string,int>::iterator iter = needWord.find(sub);
    			if(iter == needWord.end())break;
    			hasFindWord[sub]++;
    			if(hasFindWord[sub] > needWord[sub])break;
    		}
    		if(j == totalWord)	res.push_back(i);
    	}
    	return res;
    }

};
时间: 2024-08-05 04:13:42

leetcode 之 Substring with Concatenation of All Words的相关文章

[LeetCode] 030. Substring with Concatenation of All Words (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 030. Substring with Concatenation of All Words (Hard) 链接: 题目:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 代码(github):https://gi

[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 (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】Substring with Concatenation of All Words

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 interv

LeetCode 030 Substring with Concatenation of All Words

题目要求: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 i

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

[*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

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; impo