Java for LeetCode 044 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
解题思路一:参考之前写的http://www.cnblogs.com/tonyluis/p/4464059.html,可以很轻松的用递归写出代码,JAVA实现如下:
static public boolean isMatch(String s, String p) {
		if(s.length()==0){
			for(int i=0;i<p.length();i++)
				if(p.charAt(i)!=‘*‘)
					return false;
			return true;
		}
        if (p.length() == 0)
            return s.length() == 0;
        else if (p.length() == 1)
            return p.charAt(0)==‘*‘||(s.length() == 1&& (p.charAt(0) == ‘?‘ || s.charAt(0) == p.charAt(0)));
        if(p.charAt(0)!=‘*‘){
        	if(p.charAt(0)!=s.charAt(0)&&p.charAt(0)!=‘?‘)
        		return false;
        	return isMatch(s.substring(1),p.substring(1));
        }
        int index=0;
        while(index<p.length()){
        	if(p.charAt(index)==‘*‘)
        		index++;
        	else break;
        }
        if(index==p.length())
        	return true;
        p=p.substring(index);
        for(int i=0;i<s.length();i++){
        	if(isMatch(s.substring(i),p))
        			return true;
        	}
        return false;
	}

结果Time Limit Exceeded!也就是说这种类似暴力枚举的算法肯定不能通过!

解题思路二:

用两个指针indexS和indexP遍历s和p,同时用两个指针starIndex和sPosition来记录*遍历过程中*最后一次出现的位置和对应的indexP的位置,一旦s和p不匹配,就回到starIndex的位置,同时sPosition+1,接着遍历,直到遍历完整个s为止,注意边界条件,JAVA实现如下:

static public boolean isMatch(String s, String p) {
		int indexS=0,indexP=0,starIndex=-2,sPosition=-2;
		while(indexS<s.length()){
			if(starIndex==p.length()-1)
				return true;
			if(indexP>=p.length()){
				if(starIndex<0)
					return false;
				indexP=starIndex+1;
				indexS=++sPosition;
			}
			if(s.charAt(indexS)==p.charAt(indexP)||p.charAt(indexP)==‘?‘){
				indexS++;
				indexP++;
			}
			else if(p.charAt(indexP)==‘*‘){
				starIndex=indexP++;
				sPosition=indexS;
			}
			else if(starIndex>=0){
				indexP=starIndex+1;
				indexS=++sPosition;
			}
			else return false;
		}
		while(indexP<p.length()){
			if(p.charAt(indexP)!=‘*‘)
				return false;
			indexP++;
		}
		return true;
	}
时间: 2024-11-05 14:57:25

Java for LeetCode 044 Wildcard Matching的相关文章

LeetCode 044 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 p

044 Wildcard Matching

044 Wildcard Matching 这道题直观想法是用dp 复杂度 O(mn) 代码如下, 但是在用python的时候会超时 class Solution: # @param {string} s # @param {string} p # @return {boolean} def isMatch(self, s, p): m, n = len(p), len(s) dp = [False for i in range(0, n+1)] dp[0] = True for j in ra

[LeetCode][JavaScript]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 partia

【LeetCode】Wildcard Matching 串匹配 动态规划

题目:Wildcard Matching <span style="font-size:18px;">/*LeetCode WildCard matching * 题目:给定一个目标串和一个匹配串,判定是否能够匹配 * 匹配串中的定义:字符---->字符,*---->0个.1个或者多个字符,?------>对应任意一个字符 * 思路:动态规划:*:dp[i][j] = dp[i][j-1] || dp[i-1][j] * ? || s[i] == p[i]

【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 partia

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' 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,

(Java) LeetCode 44. Wildcard Matching —— 通配符匹配

Given an input string (s) and a pattern (p), 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 enti

【leetcode】Wildcard Matching(hard) ★ 大神太牛了

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 p

19.2.4 [LeetCode 44] Wildcard Matching

Given an input string (s) and a pattern (p), 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 enti