【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] :dp[i][j] = dp[i-1][j-1]
 */
package javaTrain;

public class Train23 {
	    public boolean isMatch(String s, String p) {
	        int len_s = s.length();
	        int len_p = p.length();
	        char[] sArray = new char[len_s];
	        char[] pArray = new char[len_p];
	        sArray = s.toCharArray();
	        pArray = p.toCharArray();
	        boolean dp[][] = new boolean[500][500];	//用于记录每个子问题的解
	        int lens = 0;
	        if(len_p != 0){
	            for(int i = 0;i < len_p;i++)
	                if(pArray[i] != '*') lens++;
	        }
	        if(lens > len_s) return false;

	        dp[0][0] = true;	//两个串都是空的for
	        for(int j = 1;j <= len_p;j++){
	        	if(dp[0][j-1] && pArray[j-1] == '*') dp[0][j] = true;
	        	for(int i = 1;i <= len_s;i++){
	        		if(pArray[j-1] == '*') dp[i][j] = dp[i][j-1] || dp[i-1][j];
	        		else if(pArray[j-1] == '?'||pArray[j-1] == sArray[i-1]) dp[i][j] = dp[i-1][j-1];
	        		else dp[i][j] = false;
	        	}
	        }
	        return dp[len_s][len_p];
	    }
}
</span>
时间: 2024-10-10 14:19:01

【LeetCode】Wildcard Matching 串匹配 动态规划的相关文章

[LeetCode]wildcard matching通配符实现之动态规划

前天wildcard matching没AC,今天接着搞,改用动态规划.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*&q

[LeetCode]wildcard matching通配符实现之贪心法

前天用递归LTE,昨天用动态规划LTE,今天接着搞,改用贪心法.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*"

[LeetCode]wildcard matching通配符实现TLE

leetcode这道题还挺有意思的,实现通配符,'?'匹配任意字符,'*'匹配任意长度字符串,晚上尝试了一下,题目如下: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching shoul

LeetCode: Wildcard Matching [043]

[题目] 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 funct

Leetcode: Wildcard Matching. java

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

LeetCode: Wildcard Matching 解题报告

Wildcard MatchingImplement 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)

[LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

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

[LeetCode] 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 p

[Leetcode] Wildcard Matching

*匹配任意多个字符,.匹配单个字符 关键在于*的情况,如果遇到了星号,那么需要枚举所有与星号匹配的情况,包括空串的情况 一.递归解法 1 boolean isMRecursive(String s,String p){ 2 if(s.compareTo("")==0) return p.compareTo("")==0; 3 if(p.charAt(0)=='*'){ 4 for(int i=0;i<s.length();i++){ 5 if(isM(s.su