We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3e6f524 commit 680b10eCopy full SHA for 680b10e
C++/find-longest-awesome-substring.cpp
@@ -0,0 +1,24 @@
1
+// Time: O(10 * n)
2
+// Space: O(1024)
3
+
4
+class Solution {
5
+public:
6
+ int longestAwesome(string s) {
7
+ static const int ALPHABET_SIZE = 10;
8
+ int result = 0, mask = 0;
9
+ vector<int> lookup(1 << ALPHABET_SIZE, s.length());
10
+ lookup[0] = -1;
11
+ for (int i = 0; i < s.length(); ++i) {
12
+ mask ^= 1 << (s[i] - '0');
13
+ if (lookup[mask] == s.length()) {
14
+ lookup[mask] = i;
15
+ }
16
+ result = max(result, i - lookup[mask]); // no middle
17
+ for (int d = 0; d < ALPHABET_SIZE; ++d) {
18
+ result = max(result, i - lookup[mask ^ (1 << d)]); // as middle
19
20
+ lookup[mask] = min(lookup[mask], i);
21
22
+ return result;
23
24
+};
0 commit comments