Cindy's Blog

アマゾンで働いているエンジニアの日常

LeetCode 392. Is Subsequence

Easy question

https://leetcode.com/problems/is-subsequence/

class Solution {
    public boolean isSubsequence(String s, String t) {
        int sIndex = 0;
        int tIndex = 0;
        while (sIndex < s.length() && tIndex < t.length()){
            //"abc"
        //      s
            //"ahbcgd"
       //       t
            
            int target = s.charAt(sIndex);
            if (t.charAt(tIndex) == target){
                tIndex += 1;
                sIndex += 1;
            } else {
                tIndex += 1;
            }
            
        }
        
        if (sIndex == s.length()){
            return true;
        }
        return false;
    }
}