题目描述:
解法:
具体代码如下,其中假设字符串first中没有连续的'*'。
-
#include <iostream>
-
using namespace std;
-
-
//the main function that checks if two given strings match.
-
//the first string may contain wildcard characters
-
bool match(char *first, char *second)
-
{
-
//if we reach at the end of both strings, we are done
-
if(*first == '\0' && *second == '\0')
-
{
-
return true;
-
}
-
//make sure that the characters after '*' are present in second string
-
//The function assumes that the first string will not contain two consecutive '*'
-
if(*first == '*' && *(first + 1) != '\0' && *second == '\0')
-
{
-
return false;
-
}
-
-
//if the first string constains '?', or current characters of both strings match
-
if(*first == '?' || *first == *second)
-
{
-
return match(first + 1, second + 1);
-
}
-
-
//if there is *, then there are two possibilities
-
//a) we consider current character of second string
-
//b) we ignore current character of second string
-
if(*first == '*')
-
{
-
return match(first + 1, second) || match(first, second + 1);
-
}
-
return false;
-
}
-
-
//A function to run test cases
-
void test(char *first, char *second)
-
{
-
match(first, second)?puts("Yes"):puts("No");
-
}
-
-
int main()
-
{
-
test("g*ks", "geeks"); //yes
-
test("ge?ks*", "geeksforgeeks"); //yes
-
test("&pqrs", "pqrst"); //no
-
return 0;
- }