Skip to content

Commit 177e4e7

Browse files
committed
0030 solved.
1 parent 491a3d3 commit 177e4e7

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0030)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0030 main.cpp)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/// Source : https://leetcode.com/problems/substring-with-concatenation-of-all-words/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-22
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// Sliding Window
13+
/// Time Complexity: O(word_len * |s|)
14+
/// Space Complexity: O(|s|)
15+
class Solution {
16+
public:
17+
vector<int> findSubstring(string s, vector<string>& words) {
18+
19+
int word_len = words[0].size();
20+
21+
map<string, int> words_f;
22+
for(int i = 0; i < words.size(); i ++) words_f[words[i]] ++;
23+
24+
vector<int> res;
25+
for(int start = 0; start < word_len; start ++){
26+
27+
vector<string> data;
28+
for(int i = start; i < s.size(); i += word_len)
29+
data.push_back(s.substr(i, word_len));
30+
31+
int ok_cnt = 0, l = 0, r = -1;
32+
map<string, int> cur_f;
33+
while(l < (int)data.size()){
34+
if(r + 1 < (int)data.size() && words_f.count(data[r + 1]) && cur_f[data[r + 1]] + 1 <= words_f[data[r + 1]]){
35+
cur_f[data[r + 1]] ++;
36+
37+
if(cur_f[data[r + 1]] == words_f[data[r + 1]]){
38+
ok_cnt ++;
39+
if(ok_cnt == words_f.size())
40+
res.push_back(start + l * word_len);
41+
}
42+
43+
r ++;
44+
}
45+
else{
46+
47+
if(words_f.count(data[l])){
48+
cur_f[data[l]] --;
49+
if(cur_f[data[l]] + 1 == words_f[data[l]]) ok_cnt --;
50+
}
51+
l ++;
52+
r = max(r, l - 1);
53+
}
54+
}
55+
}
56+
57+
return res;
58+
}
59+
};
60+
61+
62+
void print_vec(const vector<int>& v){
63+
for(int e: v) cout << e << ' '; cout << '\n';
64+
}
65+
66+
int main() {
67+
68+
vector<string> words1 = {"foo", "bar"};
69+
print_vec(Solution().findSubstring("barfoothefoobarman", words1));
70+
// 0 9
71+
72+
vector<string> words2 = {"word","good","best","word"};
73+
print_vec(Solution().findSubstring("wordgoodgoodgoodbestword", words2));
74+
// empty
75+
76+
vector<string> words3 = {"bar","foo","the"};
77+
print_vec(Solution().findSubstring("barfoofoobarthefoobarman", words3));
78+
// 6 9 12
79+
80+
return 0;
81+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
7676
| 027 | [Remove Element](https://leetcode.com/problems/remove-element/description/) | [solution](https://leetcode.com/problems/remove-element/solution/) | [C++](0001-0500/0027-Remove-Element/cpp-0027/) | | |
7777
| 028 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/description/) | []<br/>[缺:完整BM算法, 其他模式匹配算法] | [C++](0001-0500/0028-Implement-strStr/cpp-0028/) | | |
7878
| 029 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [solution](https://leetcode.com/problems/divide-two-integers/solution/)<br/>[缺:不使用乘法加法除法] | [C++](0001-0500/0029-Divide-Two-Integers/cpp-0029/) | | |
79-
| | | | | | |
79+
| 030 | [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [solution](https://leetcode.com/problems/substring-with-concatenation-of-all-words/solution/) | [C++](0001-0500/0030-Substring-with-Concatenation-of-All-Words/cpp-0030/) | | |
8080
| 031 | [Next Permutation](https://leetcode.com/problems/next-permutation/) | [solution](https://leetcode.com/problems/next-permutation/solution/) | [C++](0001-0500/0031-Next-Permutation/cpp-0031/) | | |
8181
| | | | | | |
8282
| 033 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [solution](https://leetcode.com/problems/search-in-rotated-sorted-array/solution/) | [C++](0001-0500/0033-Search-in-Rotated-Sorted-Array/cpp-0033/) | | |

0 commit comments

Comments
 (0)