Check if two strings match where one string contains wildcard characters

Check if two strings match where one string contains wildcard characters

Program to check if two strings match where one string contains wildcard characters is discussed here.




Check if two strings match where one string contains wildcard characters


DESCRIPTION:

Given a text and pattern string. The pattern consists of the following characters

+: It can be replaced with 0 or more occurrence of the previous character

*: Matches any sequence of characters (including the empty sequence)

?: It can be replaced with a single occurrence of any character.

The task is to determine if the string and pattern match after successfully replacing the special characters in the pattern with the above rules. Print true if the text and pattern match else print false



Test Cases:

The first string is the pattern and the second string represents the text

Sample Input 1:

String 1: Am?zo

String 2: Amazon

Sample Output 1:

FALSE

Sample Input 2:

String 1:Am?z*on

String 2:Amazkfdsaon

Sample Output 2:

TRUE




Algorithm to check if two strings match where one string contains wildcard characters

  • Input two strings where string 1 contains wildcard characters.
  • Check both the strings character by character.
  • If a character of string 1 contains a ‘*’ or ‘+’, take the next character from string 1 and check for that character in string 2. If found, continue checking the next character, else return false.
  • If the character of string 1 contains a ‘?’, check if the immediate next character of string 1 and the next character of string 2 match. If they match, proceed checking with the next character. Else, return false.
  • Continue the same process until all the characters have been traversed.




Program to check if the two strings match where one string contains wildcard characters

@@coding::1@@


Check if two strings match where one string contains wildcard characters