Simple Java Solution , using indexOf


  • 0
    S

    '''
    public class Solution {
    public boolean isSubsequence(String s, String t) {

        int index = 0 ;
        
        int nextIndex = -1;
        
        for(int i = 0 ; i<s.length() ; i++){
            
             index = t.indexOf(s.charAt(i),nextIndex+1);
             
             if(index==-1) return false;
             
             nextIndex=index;
        }
        
        return true;
        
    }
    

    }
    '''


Log in to reply
 

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