萬用字元匹配

2021-09-14 06:56:28 字數 1193 閱讀 4264

給定乙個輸入字串s 和模式p, p包含萬用字元?與星號』』, 其中輸入s包含小寫字母a-z, p包含小寫字母a-z與?和星號, ?可以匹配任一字元, 星號*可以匹配多個字元,包括空字元。

給定輸入s與p, 判斷s 與 p是否完全匹配。

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 = 「ab」

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

分析:s.size() >= p.size()。所以最多匹配s.size()次。 依次匹配,當正確和p為問號時,繼續匹配, 當p為星號時,暫停p的位移,直到s與p相同。當p匹配完畢時,檢測p的指標位置是否為p的長度。

public bool ismatch(string s, string p)  else if (p[j] == '')  else if (istar >= 0)  else return false;

} while (p[j] == '') ++j;

return j == p.size();

}

萬用字元匹配

implement wildcard pattern matching with support for and matches any single character.matches any sequence of characters including the empty sequence ...

萬用字元匹配

1.遇到s和p都是 則直接返回true 2.判斷邊界條件如果s串已經遍歷完畢,則判斷p串是否便利完畢,如果p串長度大於s串,則判斷大於部分是否都是 如果是則返回true,否則返回false 3.判斷邊界條件如果p串已經便利完畢,則判斷s串是否便利完畢,是則返回true,否則返回false 4.判斷字...

萬用字元匹配

題目 力扣 解題思路 一開始用了遞迴來做,超時了 遞迴的思路 1 當字串和模式串為空時,直接返回true。2 當字串不為空但模式串為空時,返回false 3 當字串為空時,模式串不為空,需要判斷,如果模式串全為 則返回true,否則返回false。4 當字串和模式串均不為空時,也需要判斷。先判斷第乙...