---
# System prepended metadata

title: \#392 Is Subsequence
tags: [leetcode, leetcode-easy]

---

# \#392 Is Subsequence
## *給定兩字串s, t, 判斷s的字元組合順序是否符合t字串的相同字元順序*

e.x.<br>
s = 'abc', t = 'axxbxxcxx', return true<br>
s = 'abc', t = 'axxcxxbxx', return false


## Log
 - build 20210722 by syhuang

## 初見
 - 跑迴圈逐字比對t, 當找到相同字元, s就往下個字元繼續比對; 當t比對完之後, s的比對index=s.length的話就表示順序正確, 否則false
 - 注意edge case
 - leetcode submit: runtime >80%, memory <20%(空間複雜度較高)
```javascript=
var isSubsequence = function(s, t) {
    if(s==t || s=='') return true;
    if(s.length > t.length) return false;
    let ps = 0,pt = 0;
    let compChar = s[0];
    for(; pt<t.length; pt++){
        if(compChar==t[pt]){
            ps++;
            if(ps==s.length) return true;
            compChar = s[ps];
        }
    }
    return false;
};
```
## 備註
## 參考
###### tags: `leetcode`, `leetcode-easy`