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, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9]
(order does not matter).  (Hard)

分析:

题目意思是找到所有起始位置,使得往后的若干连续字符组成的字符串 的正好是集合中所有元素组成的字符串。

自己考虑到的还是一个暴力的做法,没有超时,写起来也没那么容易。

大致思路是建一个hash存放words不同元素出现在words中的次数(开始只存放有无,后来发现words中元素可重复)

然后对s进行遍历,从每个位置开始,依次取len长度的字符串,与hash中的值比较。根据在不在hash内进行跳出循环,hash[words[j]]--等操作.

到words.size() -1 长度自然跳出循环,说明完全匹配,可以将这个i加入结果。

代码:

 1 class Solution {
 2 public:
 3     vector<int> findSubstring(string s, vector<string>& words) {
 4         int len = words[0].size();
 5         unordered_map<string, int> count;
 6         vector<int> result;
 7         if (s.size() < words.size() * len) {
 8             return result;
 9         }
10          for (int k = 0 ; k < words.size(); ++k) {
11             if (count.find(words[k]) == count.end()) {
12                 count[words[k]] = 1;
13             }
14             else {
15                 count[words[k]] ++;
16             }
17         }
18         unordered_map<string, int> save = count;
19         for (int i = 0; i <= s.size() - words.size() * len; i++) {
20             int flag = 0;
21             for (int j = i; j < i + words.size() * len  ; j += len) {
22                 string temp = s.substr(j, len);
23                 if (count.find(temp) != count.end()) {
24                     if (count[temp] > 0) {
25                         count[temp]--;
26                     }
27                     else {
28                         flag = 1;
29                         break;
30                     }
31                 }
32                 else {
33                     flag = 1;
34                     break;
35                 }
36             }
37             if (flag == 0) {
38                 result.push_back(i);
39             }
40             count = save;
41         }
42         return result;
43     }
44 };

这个代码AC了,但遍历i的复杂度太高,显然不是最优,明天再学习下更优解。

时间: 2024-10-25 10:36:11

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

[Leetcode][Python]30: Substring with Concatenation of All Words

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 30: Substring with Concatenation of All Wordshttps://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, S, and a list of words, L, that are all of the

【leetcode刷题笔记】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:Substring with Concatenation of All Words (summarize)

题目链接 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:

每日算法之二十六:Substring with Concatenation of All Words

变相的字符串匹配 给定一个字符串,然后再给定一组相同长度的单词列表,要求在字符串中查找满足以下条件的起始位置: 1)从这个位置开始包含单词列表中所有的单词,且每个单词仅且必须出现一次. 2)在出现的过程中不能出现其他的干扰单词. 3)出现的位置可能有多个. 4)单词的出现顺序不做要求. 下面是一个例子: S:"barfoothefoobarman" L:"foo","bar" 位置0是出现位置,:两个单词均出现仅出现一次,且没有干扰.同样位置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 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

[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