Wildcard Matching leetcode 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 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://blog.csdn.net/perfect8886/article/details/22689147

1     public boolean isMatch(String s, String p) {  
 2         int i = 0;  
 3         int j = 0;  
 4         int star = -1;  
 5         int mark = -1;  
 6         while (i < s.length()) {  
 7             if (j < p.length()  
 8                     && (p.charAt(j) == ‘?‘ || p.charAt(j) == s.charAt(i))) {  
 9                 ++i;  
10                 ++j;  
11             } else if (j < p.length() && p.charAt(j) == ‘*‘) {  
12                 star = j++;  
13                 mark = i;  
14             } else if (star != -1) {  
15                 j = star + 1;  
16                 i = ++mark;  
17             } else {  
18                 return false;  
19             }  
20         }  
21         while (j < p.length() && p.charAt(j) == ‘*‘) {  
22             ++j;  
23         }  
24         return j == p.length();  
25     }

 

Wildcard Matching leetcode java

时间: 2024-10-25 20:13:30

Wildcard Matching leetcode java的相关文章

Regular Expression Matching leetcode java

题目: 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 shoul

Wildcard Matching -- leetcode

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

Wildcard Matching[leetcode]直接匹配和DP

这题类似 Regular Expression Matching,但是数据比较强. 首先介绍DP的解法,回忆Regular Expression Matching,我们也用dp(i,j)表示s[0...i-1]和p[0...j-1]是否匹配 基本情况相似,但是更简单: 1. dp(0,0) = true 2. dp(0,j) = dp(0,j-1) && p[j-1] == '*' 3. dp(i,0) = false 一般情况dp(i,j): 1. dp(i-1,j-1) &&a

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 [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][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通配符实现之贪心法

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

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

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