当前位置: 首页 > news >正文

[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 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

 

此题目和 Regular Expression Matching 非常相像,但有不一样,可以对比分析

 

 

方法一,递归,不过超时了  时间复杂度 O(n!*m!),空间复杂度 O(n)

Submission Result: Time Limit ExceededMore Details 

Last executed input: "babaabbbbbaaaaabbaababbaaaaaaabbaabaabbbabbaabbbbb", "*ba**bbbb

 
class Solution {
    public:
        bool isMatch(const char *s, const char *p) 
        {   
            //cout << "==========" << endl;
            //cout << "s\t"  <<s <<endl;
            //cout << "p\t" <<p <<endl;
            if(*s == '\0')
            {   
                if(*p == '\0')
                    return true;
                while(*p != '\0')
                {   
                    if(*p != '*')
                        return false;
                    p++;
                }   

                // *p == '\0' now
                return true;
            }   

            if(*s == *p || *p == '.')
                return isMatch(s+1, p+1);
            else
            {   
                if(*p == '*')
                {   
                    s++;
                    while(*s != '\0')
                    {
                        if(isMatch(s, p))
                            return true;
                        else
                            s++;
                    }

                    // *s == '\0' now
                    return isMatch(s, p);
                }
                else
                    return false;
            }
        }
};

 方法二:迭代   迭代版,时间复杂度 O(n*m),空间复杂度 O(1)

copy from https://github.com/haoel/leetcode/blob/master/src/wildcardMatching/wildcardMatching.cpp

bool isMatch(const char *s, const char *p) {

    const char *last_s = NULL; 
    const char *last_p = NULL;
    while( *s != '\0' ){
        if (*p=='*'){
            //skip the "*", and mark a flag
            p++;
            //edge case
            if (*p=='\0') return true;
            //use last_s and last_p to store where the "*" match starts.
            last_s = s;
            last_p = p;
        }else if (*p=='?' || *s == *p){
            s++; p++;
        }else if (last_s != NULL){ 
// 如果有*出现,且当前*p和*s不相等,就把p指向原来的*下一个字符,s在原来的last_s 基础上++,同时增加last_s
// check "last_s" to know whether meet "*" before // if meet "*" previously, and the *s != *p // reset the p, using '*' to match this situation p = last_p; s = ++last_s; }else{ // *p is not wildcard char, // doesn't match *s, // there are no '*' wildcard matched before return false; } } //edge case: "s" is done, but "p" still have chars. while (*p == '*') p++; return *p == '\0'; }

 sl.isMatch("bacccbbbbb", "*ba**bbbb") 的s和p的输出:

s bacccbbbbb
p *ba**bbbb
s bacccbbbbb
p ba**bbbb
s acccbbbbb
p a**bbbb
s cccbbbbb
p **bbbb
s cccbbbbb
p *bbbb
s cccbbbbb
p bbbb
s ccbbbbb
p bbbb
s cbbbbb
p bbbb
s bbbbb
p bbbb
s bbbb
p bbb
s bbb
p bb
s bb
p b
s b
p //不相等,p重新指向last_p,s = ++last_s; 果然是高手啊
s bbbb
p bbbb
s bbb
p bbb
s bb
p bb
s b
p b

 

转载于:https://www.cnblogs.com/diegodu/p/4286858.html

相关文章:

  • Linux之编程进阶(函数、trap信号捕捉、数组、字符串处理...)
  • 子元素浮动后,父元素高度自动增加
  • 【高级数据类型2】- 8. 结构体和方法
  • 修改server 2008远程桌面端口
  • F. Multicolored Markers(数学思维)
  • 每周一练 之 数据结构与算法(Stack)
  • 逻辑回归(个人总结)-- 于俊杰
  • 工作问题总结-----付款
  • hiero_v2.0的下载安装和使用
  • LOJ#2882. 「JOISC 2014 Day4」两个人的星座(计算几何)
  • 软件实现
  • tcp的三次握手
  • 怎样提高个人素质与修养
  • echarts、higncharts折线图或柱状图显示数据为0的点
  • iOS开发的一些奇巧淫技3
  • [LeetCode] Wiggle Sort
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • input的行数自动增减
  • JavaScript 奇技淫巧
  • RxJS: 简单入门
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 如何在GitHub上创建个人博客
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 在Docker Swarm上部署Apache Storm:第1部分
  • const的用法,特别是用在函数前面与后面的区别
  • ​【已解决】npm install​卡主不动的情况
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • # C++之functional库用法整理
  • # Java NIO(一)FileChannel
  • #pragam once 和 #ifndef 预编译头
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (02)vite环境变量配置
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (9)目标检测_SSD的原理
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (六)软件测试分工
  • (转载)(官方)UE4--图像编程----着色器开发
  • (状压dp)uva 10817 Headmaster's Headache
  • **PHP二维数组遍历时同时赋值
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET 反射 Reflect
  • .NET 解决重复提交问题
  • .net通用权限框架B/S (三)--MODEL层(2)
  • :中兴通讯为何成功
  • []利用定点式具实现:文件读取,完成不同进制之间的
  • [145] 二叉树的后序遍历 js