精華區beta Marginalman 關於我們 聯絡資訊
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 又是開心水題的一天 -- 蛤? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.251.216.212 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1669883721.A.7A1.html
Rushia: 大師 12/01 16:37
hahaha021225: 大師 12/01 16:46