5 line Java


  • 0
    W
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            int dp = 0;
            for(int i=0; i<t.length() && dp<s.length(); i++){
                dp= dp + (s.charAt(dp)==t.charAt(i) ? 1 : 0);
            }
            return dp == s.length();
        }
    }
    

    dp is the length of the largest prefix of s that is a subsequence of t.substring(0, i+1)


Log in to reply
 

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