Skip to content

Commit faee286

Browse files
authored
Create 1542-find-longest-awesome-substring.js
1 parent 3a92f5f commit faee286

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const longestAwesome = function (s) {
6+
const dp = new Array(1024).fill(s.length)
7+
let res = 0,
8+
mask = 0
9+
dp[0] = -1
10+
for (let i = 0; i < s.length; ++i) {
11+
mask ^= 1 << +s.charAt(i)
12+
res = Math.max(res, i - dp[mask])
13+
for (let j = 0; j <= 9; ++j) res = Math.max(res, i - dp[mask ^ (1 << j)])
14+
dp[mask] = Math.min(dp[mask], i)
15+
}
16+
return res
17+
}

0 commit comments

Comments
 (0)