Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a3b818c

Browse files
committed
feat: 1542. Find Longest Awesome Substring, hash+状压
1 parent 91a73eb commit a3b818c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var longestAwesome = function(s) {
6+
// 状态: 10 个 0 到 10 个 1,表示 0~9 出现次数的奇偶性
7+
// mp[st] = i 表示状态 st 出现的最早下标
8+
let mp = {}, n = s.length, st = 0, ans = 1
9+
mp[0] = -1
10+
for (let i = 0; i < n; i++) {
11+
st ^= 1 << (s[i].charCodeAt(0) - '0'.charCodeAt(0))
12+
// console.log(i, st)
13+
if (mp[st] == null) mp[st] = i
14+
15+
// 偶数长度
16+
ans = Math.max(ans, i-mp[st])
17+
// 奇数长度
18+
for (let k = 0; k < 10; k++) {
19+
let su = st ^ (1 << k)
20+
if (mp[su] != null)
21+
ans = Math.max(ans, i-mp[su])
22+
}
23+
}
24+
return ans
25+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /