|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[1704. 判断字符串的两半是否相似](https://leetcode.cn/problems/determine-if-string-halves-are-alike/solution/by-ac_oier-u26p/)** ,难度为 **简单**。 |
| 4 | + |
| 5 | +Tag : 「模拟」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +给你一个偶数长度的字符串 `s` 。将其拆分成长度相同的两半,前一半为 `a` ,后一半为 `b` 。 |
| 10 | + |
| 11 | +两个字符串 相似 的前提是它们都含有相同数目的元音`('a','e','i','o','u','A','E','I','O','U')`。注意,`s` 可能同时含有大写和小写字母。 |
| 12 | + |
| 13 | +如果 `a` 和 `b` 相似,返回 `true`;否则,返回 `false` 。 |
| 14 | + |
| 15 | +示例 1: |
| 16 | +``` |
| 17 | +输入:s = "book" |
| 18 | + |
| 19 | +输出:true |
| 20 | + |
| 21 | +解释:a = "bo" 且 b = "ok" 。a 中有 1 个元音,b 也有 1 个元音。所以,a 和 b 相似。 |
| 22 | +``` |
| 23 | +示例 2: |
| 24 | +``` |
| 25 | +输入:s = "textbook" |
| 26 | + |
| 27 | +输出:false |
| 28 | + |
| 29 | +解释:a = "text" 且 b = "book" 。a 中有 1 个元音,b 中有 2 个元音。因此,a 和 b 不相似。 |
| 30 | +注意,元音 o 在 b 中出现两次,记为 2 个。 |
| 31 | +``` |
| 32 | + |
| 33 | +提示: |
| 34 | +* 2ドル <= s.length <= 1000$ |
| 35 | +* `s.length` 是偶数 |
| 36 | +s 由 大写和小写 字母组成 |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### 模拟 |
| 41 | + |
| 42 | +根据题意进行模拟即可。 |
| 43 | + |
| 44 | +为了快速判断某个字符是否为元音字母,起始先对所有元音字母进行转存。 |
| 45 | + |
| 46 | +随后对 `s` 进行遍历,使用单个变量 `cnt` 记录元音字母数量。若当前遍历到的 `c` 所在下标属于前半部分,对 `cnt` 进行自增操作,若属于后半部分,对 `cnt` 进行自减操作。 |
| 47 | + |
| 48 | +当处理完整个 `s` 后满足 `cnt = 0` 说明前半部分元音字母和后半部分元音字母数量相同。 |
| 49 | + |
| 50 | +Java 代码: |
| 51 | +```Java |
| 52 | +class Solution { |
| 53 | + public boolean halvesAreAlike(String s) { |
| 54 | + Set<Character> set = new HashSet<>(); |
| 55 | + for (char c : "aeiouAEIOU".toCharArray()) set.add(c); |
| 56 | + int n = s.length(), cnt = 0; |
| 57 | + for (int i = 0; i < n; i++) { |
| 58 | + if (!set.contains(s.charAt(i))) continue; |
| 59 | + cnt += i < n / 2 ? 1 : -1; |
| 60 | + } |
| 61 | + return cnt == 0; |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | +TypeScript 代码: |
| 66 | +```TypeScript |
| 67 | +function halvesAreAlike(s: string): boolean { |
| 68 | + let n = s.length, cnt = 0 |
| 69 | + const set = new Set<String>() |
| 70 | + for (const c of "aeiouAEIOU") set.add(c) |
| 71 | + for (let i = 0; i < n; i++) { |
| 72 | + if (!set.has(s[i])) continue |
| 73 | + cnt += i < n / 2 ? 1 : -1 |
| 74 | + } |
| 75 | + return cnt == 0 |
| 76 | +} |
| 77 | +``` |
| 78 | +Python 代码: |
| 79 | +```Python |
| 80 | +class Solution: |
| 81 | + def halvesAreAlike(self, s: str) -> bool: |
| 82 | + cnt = 0 |
| 83 | + ss = set('aeiouAEIOU') |
| 84 | + for idx, c in enumerate(s): |
| 85 | + if c not in ss: |
| 86 | + continue |
| 87 | + cnt += 1 if idx < len(s) / 2 else -1 |
| 88 | + return cnt == 0 |
| 89 | +``` |
| 90 | +* 时间复杂度:$O(C + n),ドル其中 $C$ 为元音字母数量 |
| 91 | +* 空间复杂度:$O(C)$ |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +### 最后 |
| 96 | + |
| 97 | +这是我们「刷穿 LeetCode」系列文章的第 `No.1704` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 98 | + |
| 99 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 100 | + |
| 101 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 102 | + |
| 103 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 104 | + |
0 commit comments