diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 9cbebdc6..03513391 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -196,6 +196,7 @@ | [1688. 比赛中的配对次数](https://leetcode-cn.com/problems/count-of-matches-in-tournament/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-of-matches-in-tournament/solution/gong-shui-san-xie-jian-dan-nao-jin-ji-zh-cx7a/) | 简单 | 🤩🤩🤩🤩🤩 | | [1694. 重新格式化电话号码](https://leetcode.cn/problems/reformat-phone-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/reformat-phone-number/solution/by-ac_oier-82xq/) | 简单 | 🤩🤩🤩🤩 | | [1700. 无法吃午餐的学生数量](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/) | [LeetCode 题解链接](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/solution/by-ac_oier-rvc3/) | 简单 | 🤩🤩🤩🤩🤩 | +| [1704. 判断字符串的两半是否相似](https://leetcode.cn/problems/determine-if-string-halves-are-alike/) | [LeetCode 题解链接](https://leetcode.cn/problems/determine-if-string-halves-are-alike/solution/by-ac_oier-u26p/) | 简单 | 🤩🤩🤩🤩 | | [1706. 球会落何处](https://leetcode-cn.com/problems/where-will-the-ball-fall/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/where-will-the-ball-fall/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-jz6f/) | 中等 | 🤩🤩🤩🤩 | | [1716. 计算力扣银行的钱](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-ifit/) | 简单 | 🤩🤩🤩🤩 | | [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 | diff --git "a/LeetCode/1701-1710/1704. \345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1701-1710/1704. \345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..0500527a --- /dev/null +++ "b/LeetCode/1701-1710/1704. \345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,104 @@ +### 题目描述 + +这是 LeetCode 上的 **[1704. 判断字符串的两半是否相似](https://leetcode.cn/problems/determine-if-string-halves-are-alike/solution/by-ac_oier-u26p/)** ,难度为 **简单**。 + +Tag : 「模拟」 + + + +给你一个偶数长度的字符串 `s` 。将其拆分成长度相同的两半,前一半为 `a` ,后一半为 `b` 。 + +两个字符串 相似 的前提是它们都含有相同数目的元音`('a','e','i','o','u','A','E','I','O','U')`。注意,`s` 可能同时含有大写和小写字母。 + +如果 `a` 和 `b` 相似,返回 `true`;否则,返回 `false` 。 + +示例 1: +``` +输入:s = "book" + +输出:true + +解释:a = "bo" 且 b = "ok" 。a 中有 1 个元音,b 也有 1 个元音。所以,a 和 b 相似。 +``` +示例 2: +``` +输入:s = "textbook" + +输出:false + +解释:a = "text" 且 b = "book" 。a 中有 1 个元音,b 中有 2 个元音。因此,a 和 b 不相似。 +注意,元音 o 在 b 中出现两次,记为 2 个。 +``` + +提示: +* $2 <= s.length <= 1000$ +* `s.length` 是偶数 +s 由 大写和小写 字母组成 + +--- + +### 模拟 + +根据题意进行模拟即可。 + +为了快速判断某个字符是否为元音字母,起始先对所有元音字母进行转存。 + +随后对 `s` 进行遍历,使用单个变量 `cnt` 记录元音字母数量。若当前遍历到的 `c` 所在下标属于前半部分,对 `cnt` 进行自增操作,若属于后半部分,对 `cnt` 进行自减操作。 + +当处理完整个 `s` 后满足 `cnt = 0` 说明前半部分元音字母和后半部分元音字母数量相同。 + +Java 代码: +```Java +class Solution { + public boolean halvesAreAlike(String s) { + Set set = new HashSet<>(); + for (char c : "aeiouAEIOU".toCharArray()) set.add(c); + int n = s.length(), cnt = 0; + for (int i = 0; i < n; i++) { + if (!set.contains(s.charAt(i))) continue; + cnt += i < n / 2 ? 1 : -1; + } + return cnt == 0; + } +} +``` +TypeScript 代码: +```TypeScript +function halvesAreAlike(s: string): boolean { + let n = s.length, cnt = 0 + const set = new Set() + for (const c of "aeiouAEIOU") set.add(c) + for (let i = 0; i < n; i++) { + if (!set.has(s[i])) continue + cnt += i < n / 2 ? 1 : -1 + } + return cnt == 0 +} +``` +Python 代码: +```Python +class Solution: + def halvesAreAlike(self, s: str) -> bool: + cnt = 0 + ss = set('aeiouAEIOU') + for idx, c in enumerate(s): + if c not in ss: + continue + cnt += 1 if idx < len(s) / 2 else -1 + return cnt == 0 +``` +* 时间复杂度:$O(C + n)$,其中 $C$ 为元音字母数量 +* 空间复杂度:$O(C)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1704` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +