首页 > 其他分享 >字符串--通配符*匹配

字符串--通配符*匹配

时间:2022-10-26 20:06:40浏览次数:43  
标签:SIndex string -- 通配符 PIndex Output 字符串 Input Example


44. Wildcard Matching

Hard

120177FavoriteShare

Given an input string (​​s​​​) and a pattern (​​p​​​), 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).

Note:

  • ​s​​​ could be empty and contains only lowercase letters​​a-z​​.
  • ​p​​​ could be empty and contains only lowercase letters​​a-z​​​, and characters like​​?​​​ or​​*​​.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' matches any sequence.

Example 3:

Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

Example 4:

Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".

Example 5:

Input:
s = "acdcb"
p = "a*c?b"
Output: false
class Solution {
public:
bool isMatch(string s, string p) {
int SIndex = 0,PIndex = 0,SStart = 0,PStart = 0;
while(SIndex < s.length()){
//--s和p一一匹配
if(s[SIndex] == p[PIndex] || p[PIndex] == '?'){
SIndex++;PIndex++;
}
//--遇到通配符*
else if(p[PIndex] == '*'){
PStart = ++PIndex;
SStart = SIndex;
}
//--s[SIndex] != p[PIndex],并且s[SIndex]前面有通配符*
else if(PStart > 0){
PIndex = PStart;
SIndex = ++ SStart;
}
//--s[SIndex] != p[PIndex],并且s[SIndex]前面没有通配符*
else{
return false;
}
}
while(PIndex < p.length() && p[PIndex] == '*'){
PIndex++;
}
if(PIndex == p.length()){
return true;
}
else{
return false;
}
}
};

 

标签:SIndex,string,--,通配符,PIndex,Output,字符串,Input,Example
From: https://blog.51cto.com/u_13121994/5798289

相关文章

  • 只有一对不同颜色的相邻砖块
    题目描述小易有一些彩色的砖块。每种颜色由一个大写字母表示。各个颜色砖块看起来都完全一样。现在有一个给定的字符串s,s中每个字符代表小易的某个砖块的颜色。小易想把他......
  • O(n)求三个数乘积的最大值
    题目描述给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1)输入描述:输入共2行,第一行包括一个整数n,表示数组长度......
  • 求两个字符串的最长公共子字符串长度
    题目描述给定两个字符串,请编写代码,输出最长公共子串(LongestCommonSubstring),是指两个字符串中的最长的公共子串,要求子串一定是连续。输入描述:文本格式,2个非空字符串(字母......
  • 大数乘法
    题目描述实现大数乘法,输入是两个字符串如 n1='340282366920938463463374607431768211456' n2='340282366920938463463374607431768211456' 输出 '1157920892373......
  • bitset容器找出0~n-1中重复的那个数字
    题目描述一组无序的自然数集合,由0,1,2......,n的数字和一个的数字X(X>=0&&X<=n)组成,请从集合中找出这个重复数字X。输入描述:空格分割的自然数集合输出描述:重复数字......
  • 0-1背包判断物品能否组合问题
    题目描述小米之家是成人糖果店。里面有很多便宜,好用,好玩的产品。中秋节快到了,小米之家想给米粉们准备一些固定金额大礼包。对于给定的一个金额,需要判断能不能用不同种产品(一......
  • 连续子区间的和大于等于某一个数
    题目描述小M给你一串含有c个正整数的数组, 想让你帮忙求出有多少个下标的连续区间, 它们的和大于等于x。输入描述:第一行两个整数cx(0<c<=1000000,0<=x<=100000......
  • 最长对称子字符串
    题目描述给定一个字符串(数字或大小写字母),找出最长的对称的子串(如有多个,输出任意一个)。例如:输入:“abbaad”输出:“abba”输入描述:字符串输出描述:字符串示例1输入复制......
  • BFS最短路径(求x到y的最少计算次数)
     给定两个-100到100的整数x和y,对x只能进行加1,减1,乘2操作,问最少对x进行几次操作能得到y?例如:a=3,b=11:可以通过3*2*2-1,3次操作得到11;a=5,b=8:可以通过(5-1)*2,2次操作得到......
  • 找规律
    题目描述小Q在学习许多排序算法之后灵机一动决定自己发明一种排序算法,小Q希望能将n个不同的数排序为升序。小Q发明的排序算法在每轮允许两种操作:1、 将当前序列中前n-1个数......