LeetCode Wildcard Matching

Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
超时,等会儿再优化!
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if(0 == *s)
    	{
			if(0 == *p)
				return true;
			else if('*' == *p)
				return isMatch(s, p+1);
			else
				return false;
		}
		else
		{
			if(0 == *p)
				return false;
			else if('?' == *p || *p == *s)
				return isMatch(s+1, p+1);
			else if('*' == *p)
				return isMatch(s, p+1) || isMatch(s+1, p);
			else
				return false;
		}
    }
};


终于解决了,用的是Java,感觉C++会复杂一点。
public class Solution {
    public boolean isMatch(String s, String p) {
        if(p == "")	return s == "";
    	
    	ArrayList<String> splits = splitString(p);
    	if(splits.size() == 0)	return true;
    	String start = splits.get(0);
    	if('*' != p.charAt(0))
    	{
    		if(s.length() < start.length())	return false;
    		for(int i = 0; i < start.length(); ++i)
    			if(p.charAt(i) != s.charAt(i) && '?' != p.charAt(i))
    				return false;
    		splits.remove(0);
    		s = s.substring(start.length());
    	}
    	if(!p.endsWith("*"))
    	{
    		if(splits.size() == 0)	
    			return s.equals("");
    		String end = splits.get(splits.size() - 1);
    		if(s.length() < end.length())	return false;
    		for(int i = 0; i < end.length(); ++i)
    			if(s.charAt(s.length() - 1 - i) != p.charAt(p.length() - 1 - i) && '?' != p.charAt(p.length() - 1 - i))
    				return false;
    		splits.remove(splits.size() - 1);
    		s = s.substring(0, s.length() - end.length());
    	}
    	int idx = 0;
    	for(int i = 0; i < splits.size(); ++i)
    	{
    		String part = splits.get(i);
    		while(idx <= s.length() - part.length())
    		{
    			boolean flag = true;
    			for(int x = 0; x < part.length(); ++x)
    			{
    				if(part.charAt(x) != s.charAt(idx + x) && '?' != part.charAt(x))
    				{
    					flag = false;
    					break;
    				}
    			}
    			if(flag)
    				break;
    			else
    				++idx;
    		}
    		if(idx < 0)	return false;
    		idx += splits.get(i).length();
    		if(idx > s.length())	return false;
    	}
    	return true;
    }
    
    ArrayList<String> splitString(String pattern)
    {
    	ArrayList<String> ret = new ArrayList<String>();
    	int left = -1, right = -1;
    	while(right < pattern.length())
    	{
    		while(++left < pattern.length() && '*' == pattern.charAt(left));
        	right = left;
        	while(++right < pattern.length() && '*' != pattern.charAt(right));
        	if(left < pattern.length() && right <= pattern.length() && right > left)
        		ret.add(pattern.substring(left, right));
        	left = right;
    	}
    	
    	return ret;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值