Java Straight Forward Solution


  • 0
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            int j=0;
            for(int i=0;i<s.length();i++){
                if(j==t.length()){//This if() is necessary, but may be forgotten.
                    return false;
                }
                while(t.charAt(j)!=s.charAt(i)){
                    j++;
                    if(j==t.length()){
                        return false;
                    }
                }
                j++;//Since j++ here.
            }
            return true;
        }

Log in to reply
 

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