Skip to content

Commit 63e85bb

Browse files
committed
1371
1 parent fca1c44 commit 63e85bb

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

leetcode/1371/0.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"eleetminicoworoep"

leetcode/1371/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
7+
#include "solution-2.cpp"
8+
using namespace std;
9+
10+
typedef vector<int> vi;
11+
typedef vector<vector<int>> vvi;
12+
13+
int main()
14+
{
15+
LeetCodeInput li("0.in");
16+
17+
auto l0 = li.get<string>(0);
18+
19+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
20+
Solution solution;
21+
auto output = solution.findTheLongestSubstring(l0);
22+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
23+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
24+
25+
cout << output << endl;
26+
27+
// for (int i: output){
28+
// cout << i << ' ';
29+
// }
30+
31+
// for (auto i: output){
32+
// for (int j: i){
33+
// cout << j << ' ';
34+
// }
35+
// cout << '\n';
36+
// }
37+
38+
return 0;
39+
}

leetcode/1371/solution-2.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: 40 ms, faster than 100.00% of C++ online submissions for Find the Longest Substring Containing Vowels in Even Counts.
7+
Memory Usage: 16.2 MB, less than 60.64% of C++ online submissions for Find the Longest Substring Containing Vowels in Even Counts.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
int findTheLongestSubstring(string s) {
21+
int n = size(s);
22+
int pos_by_val[32];
23+
fill_n(pos_by_val, 32, -2);
24+
pos_by_val[0] = -1;
25+
int status = 0;
26+
int ans = 0;
27+
for (int i=0; i<n; ++i){
28+
switch (s[i]){
29+
case 'a':
30+
status ^= 1;
31+
break;
32+
case 'e':
33+
status ^= 2;
34+
break;
35+
case 'i':
36+
status ^= 4;
37+
break;
38+
case 'o':
39+
status ^= 8;
40+
break;
41+
case 'u':
42+
status ^= 16;
43+
break;
44+
}
45+
if (pos_by_val[status] == -2){
46+
pos_by_val[status] = i;
47+
} else {
48+
ans = max(ans, i-pos_by_val[status]);
49+
}
50+
}
51+
return ans;
52+
}
53+
};
54+
55+
const static auto initialize = [] {
56+
std::ios::sync_with_stdio(false);
57+
std::cin.tie(nullptr);
58+
std::cout.tie(nullptr);
59+
return nullptr;
60+
}();

leetcode/1371/solution.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: 450 ms, faster than 6.64% of C++ online submissions for Find the Longest Substring Containing Vowels in Even Counts.
7+
Memory Usage: 93.9 MB, less than 5.26% of C++ online submissions for Find the Longest Substring Containing Vowels in Even Counts.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
int findTheLongestSubstring(string s) {
21+
int n = size(s);
22+
unordered_map<int, int> pos_by_val;
23+
int status = 0;
24+
int ans = 0;
25+
pos_by_val.emplace(0, -1);
26+
for (int i=0; i<n; ++i){
27+
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u'){
28+
status ^= (1<<(s[i]-'a'));
29+
}
30+
auto res = pos_by_val.emplace(status, i);
31+
if (!res.second){
32+
ans = max(ans, i-(*res.first).second);
33+
}
34+
}
35+
return ans;
36+
}
37+
};
38+
39+
const static auto initialize = [] {
40+
std::ios::sync_with_stdio(false);
41+
std::cin.tie(nullptr);
42+
std::cout.tie(nullptr);
43+
return nullptr;
44+
}();

0 commit comments

Comments
 (0)