[leetcode] Two Pointers (Easy) - 392. Is Subsequence

brandon·2025년 7월 10일

leetcode-two-pointers

목록 보기
2/2

답안

class Solution {
    public boolean isSubsequence(String s, String t) {
        int sIndex = 0, tIndex = 0; 
        if (t.length() < s.length()) {
            return false; 
        }

        if (t.length() == s.length()) {
            return s.equals(t); 
        }

        while(sIndex < s.length() && tIndex < t.length()) {
            if (t.charAt(tIndex) == s.charAt(sIndex)) {
                sIndex++; 
                tIndex++; 
            } else {
                tIndex++; 
            }
        }
        
        if (sIndex == s.length()) {
            return true; 
        }

        return false;
    }
}
profile
everything happens for a reason

0개의 댓글