- 浏览: 186652 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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 274Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 279You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 395Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 385Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 510Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 577Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 490Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 679Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 484The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 439Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 592Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 607Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 438All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 915Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 943Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 614Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 707Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 877Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 802You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 745For a undirected graph with tre ...
相关推荐
Wildcard Matching Wildcard Matching O(N) by Jimbowhy http://blog.csdn.net/WinsenJiansbomber/article/details/50862569 在 LeetCode 上看到第二个有趣的问题,是关于字符串匹配的,在接触过正则表达式后就...
标题中的“Wildcard Matching Functions Library-开源”指的是一个专门用于处理字符串和通配符匹配的开源库。这个库可能包含了各种函数,旨在帮助开发者在不同平台上实现字符串与通配符模式的有效比较,例如“*”...
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 ...
js js_leetcode题解之44-wildcard-matching.js
3. **Object Stability:** The tool supports wildcard matching of object names, allowing for changes between builds without the need to edit every script. This feature is crucial for maintaining the ...
例如,“Wildcard Matching”和“Valid Palindrome II”这类题目要求考虑通配符或特殊字符的存在,增加了问题的复杂性。在处理这类问题时,了解API的使用、忽略字符大小写和字符编码差异是十分重要的。 最后,树...
12. 字符串匹配问题:如Wildcard Matching(通配符匹配)、Flip Game(翻转游戏)等,这些题目要求熟悉字符串处理以及递归回溯等算法。 13. 字符串处理高级问题:如Scramble String(错乱字符串),验证字符串的...
- Wildcard Matching(通配符匹配) - Length of Last Word(最后一个单词的长度) - Count and Say(猜数字序列) - **Integer Array**(整型数组操作) - Remove Element(移除元素) - Zero Sum Subarray...
- Wildcard Matching: 实现支持'?'和'*'的通配符匹配。 - Jump Game / Jump Game II: 第一个问题要求判断是否能到达数组的最后一个位置,第二个问题要求求出最小跳跃次数到达最后一个位置。 - Permutations / ...
- **通配符匹配(Wildcard Matching)**: 实现通配符‘*’和‘?’匹配。 #### 数组操作 - **查找字符串中的重复元素(Remove Duplicates)**: 从排序数组中删除重复项。 - **两数之和(2 Sum)**: 给定一个整数数组 nums ...
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
首先,让我们来看一下“Problem1: Wildcard Matching”。这个问题的核心是实现一个函数,它接受两个参数:一个字符串`s`和一个包含通配符`*`的模式`p`,并判断`s`是否能通过重新排列字符匹配`p`。通配符`*`在这里...
- 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 '...
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-string-matching”,即带有通配符的字符串匹配问题,主要以Java语言为实现背景。 通配符通常用于表示一个或多个字符,常见的通配符有星号(*)和问号(?)。在字符串匹配中,星号代表零...