[LeetCode] Substring with Concatenation of All Words

简介: I think the following code is self-explanatory enough. We use an unordered_map counts to record the expected times of each word and another unordered_map seento record the times we have seen.

I think the following code is self-explanatory enough. We use an unordered_map<string, int> counts to record the expected times of each word and another unordered_map<string, int> seento record the times we have seen. Then we check for every possible position of i. Once we meet an unexpected word or the times of some word is larger than its expected times, we stop the check. If we finish the check successfully, push i to the result indexes.

 1 class Solution {
 2 public:
 3     vector<int> findSubstring(string s, vector<string>& words) {
 4         unordered_map<string, int> counts;
 5         for (string word : words)
 6             counts[word]++;
 7         int n = s.length(), num = words.size(), len = words[0].length();
 8         vector<int> indexes;
 9         for (int i = 0; i < n - num * len + 1; i++) {
10             unordered_map<string, int> seen;
11             int j = 0;
12             for (; j < num; j++) {
13                 string word = s.substr(i + j * len, len);
14                 if (counts.find(word) != counts.end()) {
15                     seen[word]++;
16                     if (seen[word] > counts[word])
17                         break;
18                 } 
19                 else break;
20             }
21             if (j == num) indexes.push_back(i);
22         }
23         return indexes;
24     }
25 };

The following is a more sophisticated solution taken from this link. You may need to spend some time understanding how it works.

 1 class Solution {
 2 public:
 3     vector<int> findSubstring(string s, vector<string>& words) {
 4         int n = s.length(), len = words[0].length(), num = words.size();
 5         unordered_map<string, int> counts;
 6         for (string word : words)
 7             counts[word]++;
 8         vector<int> indexes;
 9         for (int i = 0; i < len; i++) {
10             int left = i, valid = 0;
11             unordered_map<string, int> seen;
12             for (int j = i; j <= n - len; j += len) {
13                 string word = s.substr(j, len);
14                 if (counts.find(word) != counts.end()) {
15                     seen[word]++;
16                     if (seen[word] <= counts[word])
17                         valid++;
18                     else {
19                         while (seen[word] > counts[word]) {
20                             string wd = s.substr(left, len);
21                             seen[wd]--;
22                             if (seen[wd] < counts[wd]) valid--;
23                             left += len;
24                         }
25                     }
26                     if (valid == num) {
27                         indexes.push_back(left);
28                         seen[s.substr(left, len)]--;
29                         valid--;
30                         left += len;
31                     }
32                 }
33                 else {
34                     seen.clear();
35                     valid = 0;
36                     left = j + len;
37                 }
38             }
39         }
40         return indexes;
41     }
42 };

 

目录
相关文章
|
6月前
|
Java
Leetcode 3. Longest Substring Without Repeating Characters
此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。
29 3
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
|
索引
LeetCode 76. Minimum Window Substring
给定一个字符串S和一个字符串T,找到S中的最小窗口,它将包含复杂度为O(n)的T中的所有字符。
48 0
LeetCode 76. Minimum Window Substring
|
Java 程序员 开发者
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
本文是《LeetCode第三题(Longest Substring Without Repeating Characters)三部曲》的第二篇,前一篇文章已经列出了完整的解题思路,今天来将此思路转化为具体的Java代码
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
|
Java 程序员 开发者
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
本文是《LeetCode第三题(Longest Substring Without Repeating Characters)三部曲》的第二篇,前一篇文章已经列出了完整的解题思路,今天来将此思路转化为具体的Java代码
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
|
Java 程序员 开发者
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
本文回顾了笔者对LeetCode第三题(Longest Substring Without Repeating Characters)的解题和优化思路,希望有兴趣的读者来一起广泛讨论
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
|
索引
Leetcode-Medium 5. Longest Palindromic Substring
Leetcode-Medium 5. Longest Palindromic Substring
93 0
|
存储 Java 索引
LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 Given a string, find the length of the longest substring without repeating characters. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
973 0