My recursive solution in Java


  • 0

    The corner cases maybe not concise enough. Any modification is welcome. Anyway, my solution get an AC.

    public boolean isSubsequence(String s, String t) {
            if(s.length() == 0 && t.length() >= 0) return true;
            if(s.length() > t.length()) return false;
            
            int index = t.indexOf(s.charAt(0));
            if(index == -1) return false;
            if(s.length() == 1 && index != -1) return true;
            
            return isSubsequence(s.substring(1, s.length() - 1), t.substring(index + 1, t.length() - 1)) || 
                isSubsequence(s, t.substring(index + 1, t.length() - 1));
        }
    

Log in to reply
 

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