10. Regular Expression Matching

Description

 

 

 Solution

class Solution {
   public boolean isMatch(String s, String p) {
		// after scan the pattern, if string still has characters, return false
        if (p.isEmpty()) 
        	return s.isEmpty();
        
        // check if the first letter of string matches the pattern
        boolean first = !s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');
        
        // check if the follow letter is *
        if (p.length() >= 2 && p.charAt(1) == '*') {
        	return isMatch(s, p.substring(2)) || (first && isMatch(s.substring(1), p)); 
        } else {
        	return first && isMatch(s.substring(1), p.substring(1));
        }
    }
}

  

posted @ 2020-01-13 10:31  一锅Biger粥  阅读(84)  评论(0)    收藏  举报