I have two answers to a question on leetcode.Is Subsequence
1
Runtime: 9 ms, faster than 42.67% of Java online submissions for Is Subsequence. Memory Usage: 42.5 MB, less than 100.00% of Java online submissions for Is Subsequence.
class Solution {
    public boolean isSubsequence(String s, String t) {
        int i=0,j=0;
        while(i<s.length()&&j<t.length()){
            if(s.charAt(i)==t.charAt(j))
                ++i;
            ++j;
        }
        return i==s.length();
    }
}
2
Runtime: 1 ms, faster than 100.00% of Java online submissions for Is Subsequence. Memory Usage: 42.5 MB, less than 100.00% of Java online submissions for Is Subsequence.
class Solution {
    public boolean isSubsequence(String s, String t) {
        char[] arr = s.toCharArray();
        int index = -1;
        for(int i=0;i<s.length();++i){
            index = t.indexOf(arr[i],index+1);
            if(index<0)
                return false;
        }
        return true;
    }
}
I think indexOf() is essentially looking for index by traversing String.Inside this indexOf() still need to loop and traverse.
But why the second solution is more efficient than the first?
