일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- concurrency
- Substring with Concatenation of All Words
- 43. Multiply Strings
- LeetCode
- 프로그래머스
- Python
- 시바견
- 밴픽
- data science
- Python Code
- attribute
- Regular Expression
- Generator
- Class
- kaggle
- iterator
- Decorator
- shiba
- DWG
- 30. Substring with Concatenation of All Words
- 715. Range Module
- Protocol
- 315. Count of Smaller Numbers After Self
- 파이썬
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- t1
- Python Implementation
Archives
- Today
- Total
Scribbling
[C++] unordered_map, unordered_set, substr 본문
LeetCode: 30. Substring with Concatenation of All Words
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ret;
int length = words.at(0).size();
for (int t = 0; t < length; t++) {
unordered_map<string, int> needs;
unordered_set<string> wordSet(words.begin(), words.end());
unordered_set<string> requireds(wordSet);
for (string word : words) {
if (needs.count(word) == 0) {
needs[word] = 1;
}
else {
needs[word]++;
}
}
for (int i, j = t; j < s.size(); j += length) {
i = j - length * words.size();
if (i >= 0) {
string word = s.substr(i, length);
if (wordSet.find(word) != wordSet.end()) {
needs[word]++;
if (needs[word] > 0) {
requireds.insert(word);
}
}
}
string word = s.substr(j, length);
if (wordSet.find(word) != wordSet.end()) {
needs[word]--;
if (needs[word] == 0) {
requireds.erase(word);
}
if (requireds.empty()) {
ret.push_back(i + length);
}
}
}
}
return ret;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] priority queue (pair<int, pair<int, int>>), minheap (0) | 2023.09.02 |
---|---|
[C++] vector sum, dp table with vector (0) | 2023.09.02 |
[C++] vector sum, vector max (0) | 2023.08.31 |
[C++] ostringstream, isdigit (0) | 2023.08.29 |
[C++] to_string, deque, string split (0) | 2023.08.24 |