Simple Java solution based on recursion


  • 1
    J
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            if(s == null || s.length() == 0) return true;
            else{
                int idx = t.indexOf(s.charAt(0));
                if(idx == -1) return false;
                return isSubsequence(s.substring(1),t.substring(idx + 1));
            }
        }
    }

Log in to reply
 

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