↓快轉到主要內容
  1. LeetCode/

LeetCode 392: Is Subsequence

·1 分鐘· ·
LeetCode Easy Two-Pointers String Dynamic-Programming
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試:2022-08-23

  • 提交紀錄:Runtime Error ×2 → Accepted ×2 → Runtime Error → Accepted ×2 → Runtime Error → Wrong Answer → Accepted ×3 → Runtime Error → Wrong Answer → Runtime Error ×2 → Accepted

解題思路
#

兩個字串各一個指針(Two Pointers 模板二):j 掃過 t,t[j] == s[i] 時 i 前進。迴圈條件是 i < len(s) and j < len(t),最後 i == len(s) 就成立。這樣 s 或 t 是空字串都不用特別處理。進階題有很多個 s 時,先把 t 每個字元出現的位置存起來,對每個 s 用 binary search 找下一個位置。

我的嘗試
#

這題寫了很多次(2022、2025、2026),錯誤幾乎都在空字串和邊界:

  • 2022-08 用 queue pop(0),s 是空的時候 queue[0] 超出範圍。
  • 2022-11 寫成 if not t or not s: return False,s 是空字串時應該是 True:('', '') 回傳 False。
  • 2025 年內層 while t[r] != s[l] 在 t 是空的時直接 IndexError;補上 if not t: return False 又把 ('', '') 弄錯了;配對後 r += 1 超出範圍也沒檢查:('bb', 'b') IndexError。
  • 2026 年的版本只在不相同時檢查 right == m,t 是空字串時第一次比較就超出範圍;把邊界放進 while 條件才 AC。

正確解法
#

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        i = 0
        for c in t:
            if i < len(s) and s[i] == c:
                i += 1
        return i == len(s)

複雜度
#

時間 O(n + m),空間 O(1)。

要特別避免的錯誤
#

  • s 是空字串時答案是 True,就算 t 也是空的。
  • 存取 t[j] 之前要確認 j 沒有超出範圍,包括配對成功、j 前進之後。

面試口說整理
#

我用兩個指針,掃過 t 的每個字元,跟 s 目前的字元相同就把 s 的指針往前,最後 s 的指針走完就是子序列。時間 O(n + m)、空間 O(1)。

相關文章

LeetCode 344: Reverse String
·1 分鐘
LeetCode Easy Two-Pointers String
LeetCode 344 解題筆記:左右指針交換後往中間走,原地反轉
LeetCode 125: Valid Palindrome
·1 分鐘
LeetCode Easy Two-Pointers String
LeetCode 125 解題筆記:左右指針跳過非英數字元,再比較小寫是否相同;數字也要算進去
LeetCode 28: Find the Index of the First Occurrence in a String
·1 分鐘
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 解題筆記:在每個起點比對 needle,或直接用 str.find;index 找不到會丟例外
LeetCode 345: Reverse Vowels of a String
·1 分鐘
LeetCode Easy Two-Pointers String
LeetCode 345 解題筆記:跟 LC 917 一樣的左右指針,只是條件換成「是不是母音」(大小寫都要算)
LeetCode 283: Move Zeroes
·1 分鐘
LeetCode Easy Array Two-Pointers
LeetCode 283 解題筆記:寫入指標只在遇到非零數時前進,跟它交換;零自然被推到後面
LeetCode 290: Word Pattern
·1 分鐘
LeetCode Easy Hash-Table String
LeetCode 290 解題筆記:pattern 的字元和單字要一對一對應,用兩個方向的 dict 或比較 set 大小