class Solution {
public boolean isMatch(String s, String p) {
if (p == null || p.length() == 0) {
return s == null || s.length() == 0;
}
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
dp[0][0] = true;
int i, j; for (i = 1; i <= p.length(); i++) {
if (p.charAt(i - 1) == '*') {
dp[0][i] = true;
} else {
break;
}
} for (i = 1; i <= s.length(); i++) {
for (j = 1; j <= p.length(); j++) {
// the letter of p[j] exactly match with s[i] or is '?'
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '?') {
dp[i][j] = dp[i - 1][j - 1];
} // the letter of p is '*'
if (p.charAt(j - 1) == '*') {
/*
* dp[i][j] = dp[i][j-1] means '*' act as empty card dp[i][j] = dp[i-1][j] means
* '*' act as wild card to match with s[i]
*/
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
}
}
}
return dp[s.length()][p.length()];
}
}

参考:https://leetcode.com/problems/wildcard-matching/discuss/429337/Java%3A-Wild-card!

补充一个python的实现:

 class Solution:
def isMatch(self, s: str, p: str) -> bool:
m,n = len(s),len(p)
dp = [[False for _ in range(n+1)]for _ in range(m+1)]
dp[0][0] = True for j in range(1,n+1):
if p[j-1] == '*':
dp[0][j] = True
else:
break
for i in range(1,m+1):
for j in range(1,n+1):
if s[i-1] == p[j-1] or p[j-1] == '?':
dp[i][j] = dp[i-1][j-1]
elif p[j-1] == '*':
dp[i][j] = dp[i-1][j] or dp[i][j-1]
# print(dp)
return dp[m][n]

思路:动态规划。

举例分析:s='acdcb' p='a*c?b' 。s长度为m,p长度为n。

初始化二维数组dp,有m+1行,n+1列。

dp[0][0] = True,先设置第0行,如果p中对应字符是‘*’,则设置为True,遇到非'*'后面都设置为False。

从第1行开始,如果s与p中对应字符是相同或者p中是'?',则dp取其'左上角'元素的值。

如果p中的字符是'*',则dp判断其'上方'和'左侧'的逻辑或,(即上方或左侧是否为True)。

leetcode44的更多相关文章

  1. LeetCode44 Wildcard Matching

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  2. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  3. leetcode44:wildcard

    44. Wildcard Matching 问题描述 给定字符串s和模式p,判断字符串s是否完全符合模式p 其中字符串s只包含小写字母,模式串p包含小写字母.*.?,其中星号表示任意长度的任意字符串, ...

  4. LeetCode--44 通配符匹配

    题目就是说两个字符串,一个含有通配符,去匹配另一个字符串:输出两个字符串是否一致. 注意:’?’表示匹配任意一个字符,’*’表示匹配任意字符0或者多次 首先,我们想到暴力破解.如果从头到尾的破解,到第 ...

  5. Leetcode44. 通配符匹配(动态规划)

    44. 通配符匹配 动态规划 \(f_{i,j}\)为\(s\)匹配\(i\),\(t\)匹配\(j\)是否成功 贪心 相比之下这个思维性更强 考虑两个*,两个星号间的过渡,只需要过渡完到第二个星号, ...

随机推荐

  1. ssh调试及指定私钥

    1.ssh调试 ssh -vT username@ip[or hostname] T表示测试,v显示详细信息 也可以配置config文件(在~/.ssh/config)指定用户名和密码 如 [gerr ...

  2. Rabbitmq 安装&启动

    安装socat [root@Aliyun software]# yum -y install epel-release [root@Aliyun software]# yum -y install s ...

  3. MySQL binlog 组提交与 XA(两阶段提交)--1

    参考了网上几篇比较靠谱的文章 http://www.linuxidc.com/Linux/2015-11/124942.htm http://blog.csdn.net/woqutechteam/ar ...

  4. [UE4]Exec数据类型

    Exec是虚幻4中的一种数据类型,可以作为宏函数参数的数据类型.在宏函数库中也可以使用Exec数据类型.

  5. [UE4]获得特定类型的所有Actor:Get All Actors Of Class、Get All Actors with Interface、Get All Actors with Tag

  6. android studio AIDL 编译时 错误:找不到符号

    原贴路径:http://blog.csdn.net/baidu_30164869/article/details/51036405 PS:在android studio中 当将java文件放到aidl ...

  7. window.open 子窗口关闭刷新父页面

    function JsMod(htmlurl,tmpWidth,tmpHeight){ htmlurl=getRandomUrl(htmlurl); var winObj = window.open( ...

  8. html代码段

    添加icon<link rel="shortcut icon" href="img/100du.ico"/>

  9. spi and sensor

    http://blog.csdn.net/DroidPhone/article/details/23367051 https://www.kernel.org/doc/html/v4.14/drive ...

  10. alt.js 使用教程

    1.action : import alt from "../alt.js"; class DemoActions{ constructor() { this.generateAc ...