share my O(n+m) solution!!!


  • 0
    T
    
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            if(s==null||t==null) return s==t;
            if(s.length()>=t.length()) return s.equals(t);
            if(s.length()==0) return true;
            
            char[] s_array = s.toCharArray();
            char[] t_array = t.toCharArray();
            
            int i=0;
            int start=0;
            while(start<t_array.length){
                while(t_array[start++]!=s_array[i]){
                    if(start>=t_array.length){
                        return false;
                    }
                }
                
                i++;
                if(i==s_array.length) return true;
            }
            
            return false;
        }
    }
    
    

    ...


Log in to reply
 

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