add: 每个元音包含偶数次的最长子字符串
This commit is contained in:
parent
32fd589c3f
commit
2120bcdecf
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -10,10 +10,12 @@
|
||||
"defang",
|
||||
"defanging",
|
||||
"dvdf",
|
||||
"eleetminicoworoep",
|
||||
"lcci",
|
||||
"lcof",
|
||||
"lcov",
|
||||
"leetcode",
|
||||
"leetcodeisgreat",
|
||||
"lrloseumgh",
|
||||
"mincost",
|
||||
"nums",
|
||||
|
@ -95,6 +95,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
|
||||
- LeetCode 1108. IP 地址无效化 <https://leetcode-cn.com/problems/defanging-an-ip-address/>
|
||||
|
||||
- [每个元音包含偶数次的最长子字符串](src/string/find-the-longest-substring-containing-vowels-in-even-counts.js)
|
||||
|
||||
- LeetCode 1371. 每个元音包含偶数次的最长子字符串 <https://leetcode-cn.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/>
|
||||
|
||||
## 数组/队列/集合/映射
|
||||
|
||||
- [电话号码的字母组合](src/array/letter-combinations-of-a-phone-number.js)
|
||||
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {number}
|
||||
*/
|
||||
export const findTheLongestSubstring = function (s) {
|
||||
const n = s.length
|
||||
const pos = new Array(1 << 5).fill(-1)
|
||||
let ans = 0; let status = 0
|
||||
pos[0] = 0
|
||||
for (let i = 0; i < n; ++i) {
|
||||
const ch = s.charAt(i)
|
||||
if (ch === 'a') {
|
||||
status ^= 1 << 0
|
||||
} else if (ch === 'e') {
|
||||
status ^= 1 << 1
|
||||
} else if (ch === 'i') {
|
||||
status ^= 1 << 2
|
||||
} else if (ch === 'o') {
|
||||
status ^= 1 << 3
|
||||
} else if (ch === 'u') {
|
||||
status ^= 1 << 4
|
||||
}
|
||||
if (~pos[status]) {
|
||||
ans = Math.max(ans, i + 1 - pos[status])
|
||||
} else {
|
||||
pos[status] = i + 1
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import { findTheLongestSubstring } from '../../src/string/find-the-longest-substring-containing-vowels-in-even-counts'
|
||||
|
||||
test('每个元音包含偶数次的最长子字符串', () => {
|
||||
expect(findTheLongestSubstring('eleetminicoworoep')).toBe(13)
|
||||
expect(findTheLongestSubstring('leetcodeisgreat')).toBe(5)
|
||||
expect(findTheLongestSubstring('bcbcbc')).toBe(6)
|
||||
})
|
Loading…
Reference in New Issue
Block a user