O(n) simple iterative solution


  • 0
    M
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            if(s.length()<=0){
                return true;
            }
            if(s.length()>t.length()){
                return false;
            }
            for(int i=0;i<t.length();i++){
                if(s.length()<=0){
                    return true;
                }
                if(s.charAt(0)==t.charAt(i)){
                    s = s.substring(1, s.length());
                }
            }
            if(s.length()==0){
                return true;
            }
            return false;
        }
    }
    

Log in to reply
 

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