ubstring 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 wordsexactly once and without any intervening characters.

汗颜!!!

英语不好,这题看半天才懂题目的意思:在字符串S里找一些子串,将这些子串的下标索引返回到数组中保存。

那么子串要满足什么条件呢?  子串是由数组words里的所有单词乱序填充的,所有单词仅出现一次。

注意找到所有符合条件的子串!!!!!

看懂题目后应该就不难了。注意-----数组words的单词应该可能有重复的。

 1 class Solution {
 2 public:
 3     vector<int> findSubstring(string s, vector<string>& words) {
 4       vector<int> res;
 5       map<string,int> all;
 6       map<string,int> cur;
 7       int nums=words.size();
 8       for(int s=0;s<nums;s++)
 9           all[words[s]]++;
10       int len=words[0].size();
11       if(s.size()<len*nums) return res;
12       for(int i=0;i<=s.size()-len*nums;i++)
13        {
14            cur.clear();
15            int j;
16            for( j=0;j<nums;j++)
17            {
18                string subs=s.substr(i+j*len,len);
19                if(all.find(subs)==all.end()) break;
20                cur[subs]++;
21                if(cur[subs]>all[subs])break;
22            }
23            if(j==nums) res.push_back(i);
24        }
25        return res;
26     }
27 };
时间: 2024-09-29 01:15:17

ubstring with Concatenation of All Words的相关文章

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

LeetCode30 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 example,

[LeetCode] Substring with Concatenation of All 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 wordsexactly once and without any

【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

Substring with Concatenation of All Words leetcode

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

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 example, giv

【Substring with Concatenation of All Words】cpp

题目: 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 example,