Leetcode—44. 通配符匹配【困难】

2024每日刷题(112)

Leetcode—44. 通配符匹配

在这里插入图片描述

算法思想

在这里插入图片描述
在这里插入图片描述

实现代码

class Solution {
   
public:
    bool isMatch(string s, string p) {
   
        auto isMatchchar = [&](int i, int j)->bool {
   
            return p[j] == '?' || s[i] == p[j];
        }; 
        int m = s.size();
        int n = p.size();
        // dp[i][j] := true if s[0..i) matches p[0..j)
        // 即true if s[0...i-1] matches p[0...j-1]
        vector<vector<bool>> dp(m + 1, vector<bool>(n+1));
        dp[0][0] = true;
        for(int j = 0; j < n; j++) {
   
            if(p[j] == '*') {
   
                dp[0][j + 1] = dp[0][j];
            }
        }
        for(int i = 0; i < m; i++) {
   
            for(int j = 0; j < n; j++) {
   
                if(p[j] == '*') {
   
                    // s[0...i]和p[0...j-1]的匹配程度
                    // 如果上面为true, 即'*'匹配空字符串
                    const bool matchEmpty = dp[i+1][j];
                    // s[0...i-1]和p[0...j]的匹配程度
                    // 如果上面为true, 即'*'匹配s[i]
                    const bool matchSome = dp[i][j+1];
                    dp[i+1][j+1] = matchEmpty || matchSome;
                } else {
   
                    if(isMatchchar(i, j)) {
   
                        dp[i+1][j+1] = dp[i][j];
                    }
                }
            }
        }
        return dp[m][n];
    }
};

运行结果

在这里插入图片描述
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐

  1. 力扣44通配符匹配题解

    2024-02-06 07:30:03       57 阅读
  2. LeetCode_10_困难_正则表达式匹配

    2024-02-06 07:30:03       67 阅读
  3. 困难42. 接雨水

    2024-02-06 07:30:03       54 阅读
  4. 力扣_字符串3—通配符匹配

    2024-02-06 07:30:03       37 阅读
  5. mysql 通配符与模式匹配用法详解

    2024-02-06 07:30:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-06 07:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-06 07:30:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-06 07:30:03       82 阅读
  4. Python语言-面向对象

    2024-02-06 07:30:03       91 阅读

热门阅读

  1. 2024.02.05

    2024-02-06 07:30:03       50 阅读
  2. word导出链接

    2024-02-06 07:30:03       54 阅读
  3. 【WPF】布局容器/面板总结XAML-Panel控件

    2024-02-06 07:30:03       62 阅读
  4. leetcode 74.搜索二维矩阵

    2024-02-06 07:30:03       50 阅读
  5. I3c的上拉电阻选择

    2024-02-06 07:30:03       46 阅读
  6. React 错误边界组件 react-error-boundary 源码解析

    2024-02-06 07:30:03       61 阅读
  7. golang开源定时任务调度框架

    2024-02-06 07:30:03       53 阅读