题目来源:

  https://leetcode.com/problems/wildcard-matching/


题意分析:

  定义两个新字符规则,'?'代表任意一个字符,’*‘代表任意长度的任意字符。输入一个s和p,判断s是否能被p匹配。


题目思路:

  这题和前面的一个正则表达式类似,不过比前面那个要简单一点。“*”把p分成若干部分,然后判断,这几部分是不是可以能在s中匹配到。先定义两个指针从s和p的头出发如果p的第一个部分可以和s的前面部分匹配,那么两个指针往右移同样长度到第二部分,直到找到最后。例如,如果s为“abba”,p为“a*a”。首先,我们判断*前面那部分是不是和s的前面部分匹配。’a'和‘a'可以匹配;那么判断p中第二个'a'在s中能否匹配。发现可以,所以 返回true。这个的时间复杂度是(O(n))。


代码(python):

  

class Solution(object):
def match(self,s,p,i,j,size):
k = 0
while k < size:
if s[k + i] != p[j + k] and p[j + k] != '?':
return False
k += 1
return True def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
if s == p or p == "*":
return True
size = len(p); d = []
if size == 0 or len(s) == 0:
return False
for i in range(size):
if p[i] == '*':
d.append(i)
i = 0;j = 0;k = 0;ss = len(s)
if len(d) == 0:
if ss != size:
return False
return self.match(s,p,0,0,size)
first = True
while i < ss:
if ss - i < size - j - len(d) + k:
return False
if self.match(s,p,i,j,d[k] - j):
first = False
if k == len(d) - 1:
if d[k] == size - 1:
return True
tmp = size - 1 - d[k]
return self.match(s,p,ss - tmp,d[k] + 1,tmp)
i += d[k] - j;j = d[k] + 1;k += 1
if i == ss:
while j < size:
if p[j] != '*':
return False
j += 1
return True
elif first:
return False
else:
i += 1
return False

转载请注明出处:http://www.cnblogs.com/chruny/p/4934470.html

[LeetCode]题解(python):044-Wildcard Matching的更多相关文章

  1. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  2. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  3. LeetCode(44) Wildcard Matching

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

  4. leetcode 第43题 Wildcard Matching

    题目:(这题好难.题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)'?' Matches any single character. '*' Matches any ...

  5. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. 044 Wildcard Matching 通配符匹配

    实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...

  7. [Leetcode 44]通配符匹配Wildcard Matching

    [题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...

  8. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  9. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  10. 【一天一道LeetCode】#44. Wildcard Matching

    一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...

随机推荐

  1. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...

  2. rsyslog 传输日志

    nginx 服务器: front-end:/usr/local/nginx/logs# cat /etc/rsyslog.conf | grep -v "^$" | grep -v ...

  3. Installing perl and writing your first perl program in Ubuntu

    Installing perl and writing your first perl program in Ubuntu     Installing perl and writing your f ...

  4. Python BeautifulSoup4 使用指南

    前言: 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Python3 Win7安装 BeautifulSoup,依照里面简单的步骤就能够把Beautifu ...

  5. 优化器的使用oracle ---explain plan

    如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的每一步执行是否存在问题. 如果一条SQL平时执行的好好的,却有一天突然性能很差,如果排除了系统资源和阻塞的原因,那么基本可以 ...

  6. nginx 使用安装问题及解决方案

    如何安装清参考:http://www.runoob.com/linux/nginx-install-setup.html 可以先不用配置,等理解后在配置. 在启动nginx时,出现提示: nginx: ...

  7. Android四大组件之Activity详解

    一.Activity的概要说明 我看过Activity的源码,Activity类注释大概是这样解释的:几乎所有的Activity都是与用户交互用的,我想用了一个几乎的意思应该是排除一些纯展示界面吧,因 ...

  8. 浅谈Struts2(四)

    一.Struts2的拦截器(Intercept) 作用:把多个Action中的共有代码,提取至拦截器,从而减少Action中的冗余代码. 1.Action拦截器 a.编写interceptor类 pu ...

  9. 笔记 postgresql oid同步

    以前学习postgresql的笔记 create table 消耗 OID 如create table my_test_table, 他本身会消耗一个 会在pg_type中插入两条记录_my_test ...

  10. JSP——页面三大部分(指令、脚本、动作组件)

    一.JSP简介: JSP(Java Server Pages,Java服务器端页面开发技术) JSP可以实现的技术都可以通过Servlet实现,他们本质上是一样的.但JSP设计的目的在于简化表示层的表 ...