코딩테스트

[leetcode] 392. Is Subsequence

nayoon 2024. 5. 18. 10:19

링크

import java.util.StringTokenizer;
/**
    check that all element exists and order of the element is correct

    using two pointers, one is to check string s and the other is to check string t

    i want to use for iterator as t pointer.
    if find the element of s in string t, then move s pointer

 */
class Solution {
    public boolean isSubsequence(String s, String t) {
        String[] ss = s.split("");
        String[] st = t.split("");
        
        int index = 0;
        for(int i = 0; i < st.length; i++) {
            if (index == ss.length) {
                break;
            }
            if (st[i].equals(ss[index])) {
                index++;
            }
        }

        if (index == ss.length || s.length() == 0) {
            return true;
        } 
        return false;
    }
}