Re: [閒聊] 每日LeetCode

作者: pandix (麵包屌)   2022-12-01 16:35:18
1704. Determine if String Halves Are Alike
給你一個字串,問你他的前後兩半是否相似。
相似的定義為擁有同樣數量的母音(包含aeiouAEIOU)。
Example 1:
Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel.
Example 2:
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2.
思路:
1.切一半,Counter(),把母音數量的差距加起來看是不是0,沒了
class Solution:
def halvesAreAlike(self, s: str) -> bool:
n = len(s)
a = Counter(s[:n//2])
b = Counter(s[n//2:])
return sum([a[c] - b[c] for c in 'aeiouAEIOU']) == 0
又是開心水題的一天
作者: Rushia (みけねこ的鼻屎)   2022-12-01 16:37:00
大師
作者: hahaha021225 (安安你好)   2022-12-01 16:46:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com