题目
You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.
Two strings are alike if they have the same number of vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). Notice that s contains uppercase and lowercase letters.
Return true if a and b are alike. Otherwise, return false.
解题思路
将字符串分成两个部分,然后统计元音字母出现的次数,判断是否相等
代码
class Solution:
def halvesAreAlike(self, s: str) -> bool:
l = len(s) // 2
ct1 = Counter(s[:l].lower())
ct2 = Counter(s[l:].lower())
tmp1 = tmp2 = 0
for c in ('a', 'e', 'i', 'o', 'u'):
tmp1 += ct1[c]
tmp2 += ct2[c]
return tmp1 == tmp2