【LeetCode笔记】Regular Expression Matching

Analysis: If there is no * followed, just simple match

if there is a * followed, two scenarios: 1. * and its preceding char are not need; 2. * matches its preceding char for 0~N times


Implementations:

1. Recursive method:

bool isMatch(const char *s, const char *p) {
	//p is empty
	if(*p == '\0')
		return (*s == '\0');
		
	//p is not empty
	//a * is followed
	if(*(p + 1) == '*'){
		//take 0 preceding element
		if(isMatch(s, p + 2))
			return true;
		//take 1 or more preceding elements
		else{
			while(*s != '\0' && (*s == *p || *p == '.')){
				if(isMatch(s + 1, p + 2))
					return true;
				else
					s++;
			}
		}
	}
	//no * is followed
	else
		if(*s != '\0' && (*s == *p || *p == '.'))
			return isMatch(s + 1, p + 1);
	return false;
}
116ms


2. DP method

bool isMatch(const char *s, const char *p){
	//get lenght of s and p
	int m = 0, n = 0;
	while(s[m] != '\0')
		m++;
	while(p[n] != '\0')
		n++;
			
	//dp[i][j]: substring s[0..i-1] and p[0..j-1] match or not
	vector<vector<bool> > dp(m + 1, vector<bool>(n + 1, false));	
	dp[0][0] = true; //s[0..-1] and p[0..-1] are empty. match
	for(int i = 1; i <= m; ++i) //p is empty, s is not. never match
		dp[i][0] = false;
	for(int j = 2; j <= n; ++j)//s is empty, p is not. only p in pattern (c *)^k can match, such as a*b*c*
		if(p[j - 1] == '*' && dp[0][j - 2])
			dp[0][j] = true;
			
	//dp method			
	for(int i = 1; i <= m; ++i)
		for(int j = 1; j <= n; ++j){
			//simple match for no *
			if(p[j - 1] != '*'){
				dp[i][j] = ((s[i - 1] == p[j - 1] || p[j - 1] == '.') && dp[i - 1][j - 1]);
			}
			//two scenarios for *
			if(p[j - 1] == '*'){
				dp[i][j] = //1. do not need (c *) to match
				           (j >= 2) && dp[i][j - 2]  
				           //2. need (c *) to match
						|| dp[i][j - 1]  && (s[i - 1] == p[j - 2] || p[j - 2] == '.')  //* matches empty        
						|| dp[i - 1][j]  && (s[i - 1] == p[j - 2] || p[j - 2] == '.'); //* matches c 				
			}
		}
	return dp[m][n]; //dp[m][n]: s[0..m-1] and p[0..n-1] match or not	
}
60ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值