Simple java recursion.


  • 1
    Z

    idea from xiaohui

    class Solution {
        public boolean isMatch(String s, String p) {
            if (p.isEmpty()) return s.isEmpty();
            
            if (p.length() > 1 && p.charAt(1) == '*') {
                return isMatch(s, p.substring(2)) || 
                    (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.') && 
                     isMatch(s.substring(1), p));
            } else {
                return !s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.') && 
                    isMatch(s.substring(1), p.substring(1));
            }
        }
    }
    

Log in to reply
 

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