Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-10-07 18:29:22
※ 引述《JerryChungYC (JerryChung)》之銘言:
: https://leetcode.com/problems/minimum-string-length-after-removing-substrings
: 2696. Minimum String Length After Removing Substrings
: 有一個由大寫英文字母組成的字串 s
: 每次操作可以將字串中出現的"AB"或"CD"刪除
: 回傳結果字串的最小可能長度
: Note: 刪除子字串後字串會連接起來 可能產生新的"AB"或"CD"
: Example 1:
: Input: s = "ABFCACDB"
: Output: 2
: Explanation: 移除"AB" > 移除"CD" > 移除"AB" > 剩下"FC" 回傳2
: Example 2:
: Input: s = "ACBBD"
: Output: 5
: Explanation: 沒有可刪除的"AB"或"CD" 回傳5
: Constraints:
: 1 <= s.length <= 100
: s 只包含大寫英文字母
: 思路:刪除
: Python Code:
: class Solution:
: def minLength(self, s: str) -> int:
: while 'AB' in s or 'CD' in s:
: s = s.replace('AB', '')
: s = s.replace('CD', '')
: return len(s)
: 直接用兩次replace跟兩者各多用一次 if ... in s: 哪種比較好啊 會更快嗎
: 看了一下今天的太簡單沒人想寫 只好由最底層的我來了
思路:
一樣
Python Code:
class Solution:
def minLength(self, s: str) -> int:
while "AB" in s or "CD" in s:
s = s.replace("AB", "")
s = s.replace("CD", "")
return len(s)
刷點白癡題目回手感 最近越來越不會刷題
寫啥都卡卡的 慘
作者: Sougou (搜狗)   2023-10-07 18:29:00
哲學系的都會刷LeetCode,太卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com