Version I - two pointers - O(m + n)

public class Solution {
    public boolean isSubsequence(String s, String t) {
        if (s == null || t == null) return false;

        int i = 0;
        int j = 0;

        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;
            }
            j++;
        }

        return !(i < s.length());
    }
}

Follow up - if there are lots of incoming s1, s2, ...., sk, how to optimize

results matching ""

    No results matching ""