- 浏览: 173632 次
- 性别:
- 来自: 济南
文章分类
最新评论
Wildcard Matching
给定两个字符串p和q,判断两个字符串是否匹配。规则是:‘*’可以代表任意长度的字符串,也可以代表空串;‘?’代表任意单个的字符串。
例如:
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
这是leetcode中一道偏难的题目,我认为它属于LCS的变形,属于二维动态规划的问题。解决它我们主要要分析当前字符能代表什么。既然是二维的动态规划问题,我们创建一个二维的布尔型数组result,如果两个字符串都为空时,返回true,因此result[0][0] = true; 接着我们还要继续初始化数组的第一行,也就是result[0][i],当wildcard的当前字符为‘*’时,result[0][i]= true;当遇到不为’*'我们终止数组的初始化。
接下来我们找出状态转移方程。如果当然字符为’*‘时,我们即可以把它当做空字符也可以让它代替待匹配字符串的从当前字符开始到后面所有的字符,因此此时的动态转移方程为:result[i][j] = result[i - 1][j] || result[i][j - 1],(cur == '*');如果当前字符不为’*'时,如果cur等于待匹配字符串的当前字符或者cur等于’?‘时它们就可以匹配,与此同时,我们还要考虑上一个状态,当(cur != '*') 时的递推式为:result[i][j] = result[i - 1][j - 1] && (cur == '?' || cur == s.charAt(i - 1))。
有了初始化和递推式,代码就写出来了,代码如下:
给定两个字符串p和q,判断两个字符串是否匹配。规则是:‘*’可以代表任意长度的字符串,也可以代表空串;‘?’代表任意单个的字符串。
例如:
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
这是leetcode中一道偏难的题目,我认为它属于LCS的变形,属于二维动态规划的问题。解决它我们主要要分析当前字符能代表什么。既然是二维的动态规划问题,我们创建一个二维的布尔型数组result,如果两个字符串都为空时,返回true,因此result[0][0] = true; 接着我们还要继续初始化数组的第一行,也就是result[0][i],当wildcard的当前字符为‘*’时,result[0][i]= true;当遇到不为’*'我们终止数组的初始化。
接下来我们找出状态转移方程。如果当然字符为’*‘时,我们即可以把它当做空字符也可以让它代替待匹配字符串的从当前字符开始到后面所有的字符,因此此时的动态转移方程为:result[i][j] = result[i - 1][j] || result[i][j - 1],(cur == '*');如果当前字符不为’*'时,如果cur等于待匹配字符串的当前字符或者cur等于’?‘时它们就可以匹配,与此同时,我们还要考虑上一个状态,当(cur != '*') 时的递推式为:result[i][j] = result[i - 1][j - 1] && (cur == '?' || cur == s.charAt(i - 1))。
有了初始化和递推式,代码就写出来了,代码如下:
public class Solution { public boolean isMatch(String s, String p) { int m = s.length(); int n = p.length(); boolean[][] result = new boolean[m + 1][n + 1]; result[0][0] = true; for(int i = 1; i <= n; i++) { if(p.charAt(i - 1) == '*') result[0][i] = true; else break; } for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { char cur = p.charAt(j - 1); if(cur != '*') { result[i][j] = result[i - 1][j - 1] && (cur == '?' || cur == s.charAt(i - 1)); } else { result[i][j] = result[i - 1][j] || result[i][j - 1]; } } } return result[m][n]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 334Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 512Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 431Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 620Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 519Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 533Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 736You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
Wildcard Matching Wildcard Matching O(N) by Jimbowhy http://blog.csdn.net/WinsenJiansbomber/article/details/50862569 在 LeetCode 上看到第二个有趣的问题,是关于字符串匹配的,在接触过正则表达式后就...
简单的平台独立库,用于将字符串与通配符掩码进行比较。
9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design ...
Examples of wildcard matching 26 Overview of the development provisioning process 27 Developing an App 30 Figure 4-1 Figure 4-2 Figure 4-3 Figure 4-4 The development process 30 The development process...
Wildcard Matching Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 ...
wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map...
Wildcard Matching Python 2016/4/1 Hard 051 N-Queens C++ 2016/5/18 Hard 092 Reverse Linked List II Python 2017/11/28 Medium 095 Unique Binary Search Trees Python 2017/11/28 Medium 09
力码解决方案 Leetcode是一个网站,人们——主要是软件工程师——练习他们的编码技能。 有 800 多个问题(并且还在不断...├── Wildcard Matching │ ├── Readme.md │ └── solution.js ├── Valid Number │
leetcode中国 买卖股票 从easy一直问到hard 数独 从medium的判断是不是valid ...Wildcard Matching 判断source string和pattern string是否match, ?代表任意一个字母,比如 s = "Happy" p = "H?pp?" 这种情况
leetcode 答案 算法心得 大纲 解算法 = 思路->思路验证->直译->结果验证 进步 = 解算法->看高手答案->临摹->形成后续TODO ...容易跑偏,要直译,要直译,要直译!...很多都是可以直接求出来的...No.44(wildcard matching) No
通配符匹配leetcode Greedy-5 Problem1: Wildcard Matching () Problem2: Bikes in a Campus ()
import wildcard from "wildcard-named" ; 基本例子 import wildcard from "wildcard-named" ; wildcard ( "//blog.com/page/14" , "//blog.com/page/[digit:page]" ) ; // { 'page': '14' } wildcard ( "abc-123:d2...
wildcard-matchingpackage io.lcalmsky.leetcode. wildcard_matchingclass io.lcalmsky.leetcode. wildcard_matching.Solutiontest io.lcalmsky.leetcode. wildcard_matching.SolutionTest问题清
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...
通配符字符串匹配字符串匹配,其中一个字符串包含通配符( )
主要给大家介绍了关于spring配置文件解析失败报”cvc-elt.1: 找不到元素 'beans' 的声明”异常的解决方法,需要的朋友可以参考下
dubbo找不到dubbo.xsd报错, cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...