Skip to content

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
string vowels = "aeiou";
2+
class Solution {
3+
public:
4+
int findTheLongestSubstring(string s) {
5+
int n = s.size();
6+
int ans = 0;
7+
unordered_map<char, vector<int>> pref = {};
8+
for (char v: vowels) pref[v] = {0};
9+
10+
for(char cur: s) {
11+
for(char v: vowels) {
12+
int last_parity = *pref[v].rbegin();
13+
if (cur == v) pref[v].push_back(1 - last_parity);
14+
else pref[v].push_back(last_parity);
15+
}
16+
}
17+
18+
auto get_column = [&](int j) { // 1 based
19+
int id = 0;
20+
for(char v: vowels) {
21+
int x = pref[v][j];
22+
id = 2*id + x;
23+
}
24+
return id;
25+
};
26+
27+
int rightmost[33];
28+
rightmost[0] = 0;
29+
for (int i = 1; i <= n; i++) {
30+
int id = get_column(i);
31+
rightmost[id] = i;
32+
}
33+
34+
for (int i = 1; i <= n; i++) {
35+
// find the rightmost j substring(i, j) is valid
36+
// ith column
37+
int id = get_column(i-1);
38+
int j = rightmost[id];
39+
if (j < i) continue;
40+
ans = max(ans, j-i+1);
41+
}
42+
return ans;
43+
}
44+
};
45+
46+
/*
47+
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
48+
i ^ ^
49+
maintain prefix count of all vowels pref[a][7]
50+
a,b,a,e,e,a,e
51+
i j
52+
a - 0,1,1,0,0,0,1,1
53+
e - 0,0,0,0,1,0,0,1
54+
i - ...,1,.....,1
55+
o - ...,0,.....,1
56+
u - ...,0,.....,0
57+
10100 <-- 20
58+
n^2 substrings -> O(n) => overall O(n^3)
59+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
int countSubstrings(string s) {
4+
int n = s.size();
5+
vector<vector<bool>> dp(n, vector<bool>(n, 0));
6+
for(int i = 0; i < n; i++)
7+
dp[i][i] = 1;
8+
9+
// fill this dp
10+
for(int len = 2; len <= n; len++) {
11+
for(int i = 0; i + len - 1 < n; i++) {
12+
// starting from i, and length len
13+
int j = i + len - 1;
14+
if (i+1 == j) {
15+
dp[i][j] = s[i] == s[j];
16+
}
17+
else {
18+
dp[i][j] = s[i] == s[j] && dp[i+1][j-1];
19+
}
20+
}
21+
}
22+
23+
int ans = 0;
24+
for(int i = 0; i < n; i++)
25+
for(int j = 0; j < n; j++)
26+
ans += dp[i][j];
27+
return ans;
28+
}
29+
};
30+
/*
31+
for substring in all_subtrings;
32+
if palindrome(substring)
33+
ans++;
34+
35+
36+
dp[i][j] = substring(i, i+1, ..., j) = is this a palindrome
37+
38+
s[i] == s[j] && dp[i+1][j-1] should be true
39+
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
bool matches(string word, string pattern) {
3+
int n = word.size();
4+
vector<int> forward(26, -1), backward(26, -1);
5+
// map<char, char> forward, backward; unordered_map
6+
for(int i = 0; i < n; i++) {
7+
int x = word[i] - 'a', y = pattern[i] - 'a';
8+
// I am trying to map x to y
9+
if(forward[x] != -1 && forward[x] != y) return false;
10+
if(backward[y] != -1 && backward[y] != x) return false;
11+
forward[x] = y;
12+
backward[y] = x;
13+
}
14+
15+
return true;
16+
}
17+
18+
public:
19+
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
20+
vector<string> ans = {};
21+
// O(n * k) k is avg word size
22+
for (auto word: words) {
23+
if (matches(word, pattern)) ans.push_back(word);
24+
}
25+
26+
return ans;
27+
}
28+
};
29+
30+
/* abb xyy yxx
31+
32+
// abbccc
33+
// matches - xyyzzz yzzxxx
34+
// does not match - xyyxxx
35+
36+
abbccc yzzxxx
37+
abbccc xyyxxx
38+
39+
if a is mapped to y, then -
40+
- a shall never be mapped to anything else
41+
- no other char shall be mapped to y
42+
43+
math background -
44+
1:1 mapping
45+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
string get_normalized(string email) {
3+
int i;
4+
string local_name = "";
5+
bool ignore_chars = false;
6+
for(i = 0; i < email.size(); i++) {
7+
char c = email[i];
8+
if (c == '@') {
9+
break;
10+
}
11+
12+
if (c == '.' || ignore_chars) continue;
13+
if (c == '+') {
14+
ignore_chars = true;
15+
continue;
16+
}
17+
18+
local_name += c;
19+
}
20+
string domain = email.substr(i);
21+
return local_name + domain;
22+
}
23+
public:
24+
int numUniqueEmails(vector<string>& emails) {
25+
set<string> email_set;
26+
for(string email: emails) {
27+
email_set.insert(get_normalized(email));
28+
}
29+
30+
return email_set.size();
31+
}
32+
};

0 commit comments

Comments
 (0)