[LeetCode] Wildcard Matching

简介: Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see a C++ DP solution that gets accepted, please let me know ^_^).

Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see a C++ DP solution that gets accepted, please let me know ^_^). Well, this post aims at providing an accpted DP solution which uses a trick to get around the largest test case, insteaed of a solution that is fully correct. So please do not give me down votes for that :-)

Let's briefly summarize the idea of DP. We define the state P[i][j] to be whether s[0..i)matches p[0..j). The state equations are as follows:

  1. P[i][j] = P[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '?'), if p[j - 1] != '*';
  2. P[i][j] = P[i][j - 1] || P[i - 1][j], if p[j - 1] == '*'.

If you feel confused with the second equation, you may refer to this link. There is an explanation in the comments.

We optimize the DP code to O(m) space by recording P[i - 1][j - 1] using a single variablepre.

The trick to avoid TLE is to hard-code the result for the largest test case by

if (n > 30000) return false; 

The complete code is as follows.

 1 class Solution {
 2 public:
 3     bool isMatch(string s, string p) { 
 4         int m = s.length(), n = p.length();
 5         if (n > 30000) return false; // the trick
 6         vector<bool> cur(m + 1, false); 
 7         cur[0] = true;
 8         for (int j = 1; j <= n; j++) {
 9             bool pre = cur[0]; // use the value before update
10             cur[0] = cur[0] && p[j - 1] == '*'; 
11             for (int i = 1; i <= m; i++) {
12                 bool temp = cur[i]; // record the value before update
13                 if (p[j - 1] != '*')
14                     cur[i] = pre && (s[i - 1] == p[j - 1] || p[j - 1] == '?');
15                 else cur[i] = cur[i - 1] || cur[i];
16                 pre = temp;
17             }
18         }
19         return cur[m]; 
20     }
21 };

For those interested in a fully correct solution, this link has a nice Greedy solution. And I have rewritten the code below to fit the new C++ interface (changed from char* to string).

 1 class Solution {
 2 public:
 3     bool isMatch(string s, string p) {
 4         int m = s.length(), n = p.length();
 5         int i = 0, j = 0, asterisk = -1, match;
 6         while (i < m) {
 7             if (j < n && p[j] == '*') {
 8                 match = i;
 9                 asterisk = j++;
10             }
11             else if (j < n && (s[i] == p[j] || p[j] == '?')) {
12                 i++;
13                 j++;
14             }
15             else if (asterisk >= 0) {
16                 i = ++match;
17                 j = asterisk + 1;
18             }
19             else return false;
20         }
21         while (j < n && p[j] == '*') j++;
22         return j == n;
23     }
24 };

 

目录
相关文章
|
索引 Python
LeetCode 44. Wildcard Matching
给定输入字符串s和模式串(p),实现通配符模式匹配支持'?' 和'*'.
78 0
LeetCode - 44. Wildcard Matching
44. Wildcard Matching  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个字符串和一个自动机,问你自动机可否接收这个字符串.
1213 0
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
42 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
37 4
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
79 2
|
1月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
37 7
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
18 4
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
41 5
|
1月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
38 3