Open sync-by-unito[bot] opened 3 years ago
➤ Nelson 3513 commented:
//3. Longest Substring Without Repeating Characters [Med]
class Solution_q3 {
public:
int lengthOfLongestSubstring(std::string s) {
std::unordered_set
➤ Nelson 3513 commented:
//30. Substring with Concatenation of All Words [Hard]
class Solution {
public:
std::vector
//step1: put all words into a hashmap
int num = words.size(), len = words[0].size();
std::vector<int> res;
std::unordered_map<std::string, int> map;
for (std::string word : words)
map[word]++;
//step2: traverse s, each time take one substring to compare.
for (int i = 0; i < s.size() - num*len + 1; ++i) {
std::unordered_map<std::string, int> seen;
int j = 0;
for (; j < num; ++j) {
std::string str = s.substr(i+j*len, len);
if (map.find(str) != map.cend()) {
seen[str]++;
if (seen[str] > map[str])
break;
}
else break;
}
if (j == num) res.push_back(i);
}
return res;
}
};
➤ Nelson 3513 commented:
//438. Find All Anagrams in a String [Med]
class Solution_q438 {
public:
std::vector
//here window is moving from left to right across the string.
//window size is p.size(), so s.size()-p.size() moves are made
for (int i = p.size(); i < sn; ++i) {
//sliding window extend one char to the right.
++sv[s[i] - 'a'];
//sliding window remove one char from the left.
--sv[s[i - p.size()] - 'a'];
//since both vectors are of fixed size 26. Total complexity O(n)*O(1) = O(n)
if (sv == pv) res.push_back(i - p.size() + 1);
}
return res;
}
};
➤ Nelson 3513 commented:
//76. Minimum Window Substring
class Solution_q76 {
public:
string minWindow(string s, string t) {
if (s.size() == 0 || t.size() == 0) return "";
vector
FB Questions:
Classics:
┆Issue is synchronized with this Trello card by Unito