Leetcode 2124 Check if All A's Appears Before All B's: 所有B在A之後

 Leetcode 2124  Check if All A's Appears Before All B's: 所有B在A之後


這題是簡單題目 給一個字串,確認所有b都在a之後發生:

定義一個seen 當作指標

如過已經看過b就更新seen

不然就沒看過:


如果看到a 但是seen 是true

表示過去曾經看過b 就return False


通過所有測試則return True

class Solution:
    def checkString(self, s: str) -> bool:
        
        seen=False
        for i in s:
            if i =="b":
                seen=True
            if i=="a" and seen==True:
                return False
            
        return True

留言

這個網誌中的熱門文章

1041. Robot Bounded In Circle 機器人在圈圈裏面?

382. Linked List Random Node: 隨機選元素從list裡面

2134. Minimum Swaps to Group All 1's Together II 使得所有1連在一起