Simple Java Solution - O(t)


  • 0
    C
    public boolean isSubsequence(String s, String t) {
            char [] sArray = s.toCharArray();
            char [] tArray = t.toCharArray();
            int sIdx = 0, tIdx = 0;
            
            while (tIdx < tArray.length && sIdx < sArray.length) {
                if (tArray[tIdx++] == sArray[sIdx]) {
                    sIdx++;
                }
            }
            
            return sIdx == sArray.length;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.