Given two strings where first string may contain wild card characters and second string is a normal string. Write a function that returns true if the two strings match. The following are allowed wild card characters in first string.
* --> Matches with 0 or more instances of any character or set of characters. ? --> Matches with any one character.
For example, “g*ks” matches with “geeks” match. And string “ge?ks*” matches with “geeksforgeeks” (note ‘*’ at the end of first string). But “g*k” doesn’t match with “gee” as character ‘k’ is not present in second string.
Yes Yes No No Yes No Yes Yes
1) In the above solution, all non-wild characters of first string must be there is second string and all characters of second string must match with either a normal character or wildcard character of first string. Extend the above solution to work like other pattern searching solutions where the first string is pattern and second string is text and we should print all occurrences of first string in second.
2) Write a pattern searching function where the meaning of ‘?’ is same, but ‘*’ means 0 or more occurrences of the character just before ‘*’. For example, if first string is ‘a*b’, then it matches with ‘aaab’, but doesn’t match with ‘abb’.
This article is compiled by Vishal Chaudhary and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Wildcard Pattern Matching
- WildCard pattern matching having three symbols ( * , + , ? )
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Transform string str1 into str2 by taking characters from string str3
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Minimal moves to form a string by adding characters or appending string itself
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Count of times second string can be formed from the characters of first string
- Find Kth largest string from the permutations of the string with two characters
- Find the smallest window in a string containing all characters of another string
- Remove characters from the first string which are present in the second string
- Check whether second string can be formed from characters of first string
- String matching with * (that matches with any) in any of the two strings