Regular Expression Matching

2015-07-20 17:31:33 · 作者: · 浏览: 4

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

答案

public class Solution {
    public boolean match(char s,char p)
    {
        return s==p||p=='.';
    }
    public boolean isMatch(String s, String p)
    {
        int sLen = s.length();
        int pLen = p.length();
        int i;
        int j;
        int k;
        boolean[][] match = new boolean[sLen+1][pLen+1];
        match[0][0]=true;
        for(i=0;i