Java 7 lines solution 38ms

  • 2

    Very simple. Just use two pointers, one for each string.

    public class Solution {
        public boolean isSubsequence(String s, String t) {
            int p1 = 0, p2 = 0;
            while(p1 < s.length() && p2 < t.length()){
                if(s.charAt(p1) == t.charAt(p2))
            return p1 == s.length();

  • 0

    I have similar code in C but when I submit I get time limit Exceeded for the input where "t" is a huge string. Any suggestions ?

Log in to reply

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