精華區beta Marginalman 關於我們 聯絡資訊
https://leetcode.com/problems/determine-if-string-halves-are-alike 1704. Determine if String Halves Are Alike 給一個偶數長度的字串s,把它分成兩半。如果兩半的母音數量相同則為Alike,傳回True Example 1: Input: s = "book" Output: true Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike. Python Code: --------------------------------------------------- class Solution: def halvesAreAlike(self, s: str) -> bool: a = 0 vowels = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]) for i in range(len(s) // 2): if s[i] in vowels: a += 1 if s[-i-1] in vowels: a -= 1 return a == 0 --------------------------------------------------- 想說等人發結果都沒有 窩只剩這種Easy題可以來騙文章數了 :( 話說vowels有沒有set有差嗎 原本自己寫是沒加 不過也沒贏到哪去 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.8.224 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1705081705.A.DF3.html
JerryChungYC: https://i.imgur.com/DxffAQD.jpeg 01/13 01:58
Rushia: n很小用set反而會拖效能吧 01/13 02:43