当前位置: 首页 > 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


实现字符串匹配。输入字符串s,和字符串匹配p,?可被替换为任何非空字符,*可替换空字符或任何字符。
设,arr[i,j]表示s的第i个字符是否和p的第j个字符匹配。对于s[i]和p[j],分为三种情况:
arr[i,j] = arr[i-1,j-1] ,p[j]=?
arr[i,j] = arr[i-1,j-1] && s[i-1] == p[j-1] ,p[j]为普通字符
arr[i,j] = arr[i-1,j-1]||arr[i-1,j]||arr[i,j-1] ,p[j]为*


实现代码:


public class Solution {
    public bool IsMatch(string s, string p) {
        var dp = new bool[s.Length + 1, p.Length + 1]; 
    	dp[0,0]  = true; // s is empty , pattern is empty, match
    	
    	// s is not empty , patter is empty , not match
    	for (var i = 0;i < s.Length; i++){
    		dp[i+1,0] = false;
    	}
    	
    	// pattern not empty, s is empty , not match
        for (var i = 0;i < p.Length; i++){
    		dp[0, i+1] = p[i] == '*' && dp[0, i];
    	}
    	
    	for (var i = 1; i <= s.Length; i++){
    		for (var j = 1;j <= p.Length; j++){
    			if (p[j-1] == '?'){
    				dp[i,j] = dp[i-1,j-1]; // depends on previous match or no
    			}
    			else if(p[j-1] == '*'){
    				// 1. ab a*
    				// 2. bavfdc b*
    				// pattern j matches string i - 1 (* is any char)
    				// or
    				// pattern j-1 matches string i (* can be removed)
    				//Console.WriteLine(i+","+j);
    				dp[i,j] = dp[i-1,j] || dp[i, j-1] || dp[i-1,j-1];
    			}
    			else{
    				// pattern is a normal charactor , previous match also current char should match
    				dp[i,j] = dp[i-1,j-1] && s[i-1] == p[j-1];
    			}
    		}
    	}
    	//Console.WriteLine(dp);
    	return dp[s.Length,p.Length];
    }
}


相关文章:

  • 弥平“第三道鸿沟”:3G运营商必须承担的社会责任
  • 使用面向对象重构之-从过程式设计到面向对象
  • Bing API初体验
  • 使用面向对象重构之-继承中的抽象—模板方法
  • www.hellocpp.net开发日记:网站性能优化之文件服务器分离技术
  • 使用面向对象重构之-使用接口完成行为抽象
  • Flex与.NET互操作(十):FluorineFx.Net的及时通信应用(ApplicationAdapter)(一)
  • 使用面向对象重构之-使用接口抽象完成不同维度的扩展
  • Flex与.NET互操作(十一):FluorineFx.Net的及时通信应用(Remote Procedure Call)(二)
  • 使用面向对象重构之-把抽象控制在一处:提高内聚性
  • 输出集合所有子集的算法
  • 使用面向对象重构之-依赖注入
  • 做了Nebula3的应用程序向导
  • C# Volatile
  • Windows配置Scikit-learn 学习环境
  • 【笔记】你不知道的JS读书笔记——Promise
  • 【译】理解JavaScript:new 关键字
  • C学习-枚举(九)
  • es6--symbol
  • FastReport在线报表设计器工作原理
  • Mithril.js 入门介绍
  • Next.js之基础概念(二)
  • Vue.js-Day01
  • 从零开始的无人驾驶 1
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 警报:线上事故之CountDownLatch的威力
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 通过git安装npm私有模块
  • 项目实战-Api的解决方案
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • Python 之网络式编程
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • #pragma预处理命令
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (10)STL算法之搜索(二) 二分查找
  • (floyd+补集) poj 3275
  • (分布式缓存)Redis哨兵
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (九十四)函数和二维数组
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • ***通过什么方式***网吧
  • ... 是什么 ?... 有什么用处?
  • .NET Core 2.1路线图
  • .Net 垃圾回收机制原理(二)
  • .NET国产化改造探索(一)、VMware安装银河麒麟
  • /etc/motd and /etc/issue
  • @Resource和@Autowired的区别
  • [AIGC] MySQL存储引擎详解
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)
  • [C++][数据结构][算法]单链式结构的深拷贝
  • [codevs1288] 埃及分数
  • [Flutter] extends、implements、mixin和 abstract、extension的使用介绍说明
  • [Hive] 常见函数
  • [IMX6DL] CPU频率调节模式以及降频方法