[LeetCode][JAVA] 392. Is Subsequence

탱귤생귤·2023년 12월 20일
0

LeetCode

목록 보기
10/16

https://leetcode.com/problems/is-subsequence/?envType=study-plan-v2&envId=leetcode-75

class Solution {
    public boolean isSubsequence(String s, String t) {
        //if s is null
        if (s.length() == 0) return true;
        //if t is shorter than s
        if (s.length() > t.length()) return false;

        char[] sChar = s.toCharArray();
        char[] tChar = t.toCharArray();

        int i = 0;
        int j = 0;
        while (i < s.length() && j < t.length()) {
            if (sChar[i] == tChar[j]) i++;
            j++;

        }//while

        return i == s.length();
    }
}

I used two pointers.

0개의 댓글