Java easy-understanding recursion solution 38ms


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

    Very straightforward.


  • 0
    A

    Very elegant!


  • 0
    J

    @cdpiano Recursion is very powerful. can be used in many cases. but for this one, it is over killed. anyway, I still like it.


Log in to reply
 

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