From 2120bcdecf11c3337bd98dfe2efaf29252cb6510 Mon Sep 17 00:00:00 2001 From: yige Date: Thu, 21 May 2020 00:08:57 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=AF=8F=E4=B8=AA=E5=85=83=E9=9F=B3?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E5=81=B6=E6=95=B0=E6=AC=A1=E7=9A=84=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 2 ++ README.md | 4 +++ ...string-containing-vowels-in-even-counts.js | 30 +++++++++++++++++++ ...g-containing-vowels-in-even-counts.test.js | 7 +++++ 4 files changed, 43 insertions(+) create mode 100644 src/string/find-the-longest-substring-containing-vowels-in-even-counts.js create mode 100644 test/string/find-the-longest-substring-containing-vowels-in-even-counts.test.js diff --git a/.vscode/settings.json b/.vscode/settings.json index ea6a377..808d888 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,10 +10,12 @@ "defang", "defanging", "dvdf", + "eleetminicoworoep", "lcci", "lcof", "lcov", "leetcode", + "leetcodeisgreat", "lrloseumgh", "mincost", "nums", diff --git a/README.md b/README.md index 1894e0b..47d043c 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 1108. IP 地址无效化 +- [每个元音包含偶数次的最长子字符串](src/string/find-the-longest-substring-containing-vowels-in-even-counts.js) + + - LeetCode 1371. 每个元音包含偶数次的最长子字符串 + ## 数组/队列/集合/映射 - [电话号码的字母组合](src/array/letter-combinations-of-a-phone-number.js) diff --git a/src/string/find-the-longest-substring-containing-vowels-in-even-counts.js b/src/string/find-the-longest-substring-containing-vowels-in-even-counts.js new file mode 100644 index 0000000..bccc8c5 --- /dev/null +++ b/src/string/find-the-longest-substring-containing-vowels-in-even-counts.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 +} diff --git a/test/string/find-the-longest-substring-containing-vowels-in-even-counts.test.js b/test/string/find-the-longest-substring-containing-vowels-in-even-counts.test.js new file mode 100644 index 0000000..3cd65f4 --- /dev/null +++ b/test/string/find-the-longest-substring-containing-vowels-in-even-counts.test.js @@ -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) +})