Skip to content

Commit 9b6f4b4

Browse files
authored
Update 1542-find-longest-awesome-substring.js
1 parent 7778473 commit 9b6f4b4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1542-find-longest-awesome-substring.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@ const longestAwesome = function (s) {
1515
}
1616
return res
1717
}
18+
19+
// another
20+
21+
/**
22+
* @param {string} s
23+
* @return {number}
24+
*/
25+
const longestAwesome = function(s) {
26+
const n = s.length, { max, min } = Math
27+
const dp = Array(2 ** 10).fill(n)
28+
let res = 0, mask = 0
29+
dp[0] = -1
30+
for(let i = 0; i < n; i++) {
31+
mask ^= (1 << parseInt(s[i]))
32+
res = max(res, i - dp[mask])
33+
for(let j = 0; j <= 9; j++) {
34+
const tmp = mask ^ (1 << j)
35+
res = max(res, i - dp[tmp])
36+
}
37+
dp[mask] = min(i, dp[mask])
38+
}
39+
40+
return res
41+
};

0 commit comments

Comments
 (0)