// Algorithm / 演算法:
// 雙指針 i 走 s、j 走 t；t[j] == s[i] 時 i 前進，j 每步都前進。
// Two pointers: i over s, j over t. Advance i only on a match; j always advances.
// Returns true once i reaches the end of s (every char matched in order).

#include <stdbool.h>   // 引入 bool / true / false 型別 / brings in the bool type
#include <string.h>    // 引入 strlen / brings in strlen

bool isSubsequence(char* s, char* t) {
    size_t n = strlen(s);          // n = s 的長度，size_t 是無號整數 / length of s; size_t is unsigned
    size_t m = strlen(t);          // m = t 的長度 / length of t
    size_t i = 0;                  // i 指向 s 中下一個要匹配的字元 / index of next char in s to match
    size_t j = 0;                  // j 指向 t 中目前要檢查的字元 / index of current char in t

    // 當兩個字串都還沒掃完時繼續 / loop while both pointers are still in range
    while (i < n && j < m) {
        // s[i] 取出 s 的第 i 個字元（等價於 *(s + i)） / read the i-th char of s
        if (s[i] == t[j]) {
            i++;                   // 找到匹配，i 前進去找 s 的下一個字元 / matched: move to next char of s
        }
        j++;                       // 不論匹配與否，t 的這個位置都用過了 / either way, this t position is done
    }

    // 若 i 已經走到 s 結尾，代表 s 每個字元都依序找到 / if i reached end of s, all chars were matched in order
    return i == n;
}
