# String matching with * (that matches with any) in any of the two strings

You are given two strings A and B. Strings also contains special character * . you can replace * with any alphabetic character. Finally, you have to tell whether it is possible to make both string same or not.

Examples:

```Input : A = "gee*sforgeeks"
B = "geeksforgeeks"
Output :Yes

Input :A = "abs*"
B = "abds"
Output :No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Explanation: How we can solve above problem, Basically we three cases,
Case 1: Both string contains * at particular position, at that time we can replace both * with any character make the string equal at that position.
Case 2: If one string have character and other has * at that position. So, we can replace * with the same character in other string.
Case 3: If both the string has a character at that position, then they must be same, otherwise we can’t make them equal.

 `// CPP program for string matching with * ` `#include ` `using` `namespace` `std; ` ` `  `bool` `doMatch(string A, string B) ` `{ ` `    ``for` `(``int` `i = 0; i < A.length(); i++)  ` ` `  `        ``// if the string don't have * ` `        ``// then character at that position ` `        ``// must be same. ` `        ``if` `(A[i] != ``'*'` `&& B[i] != ``'*'``)  ` `            ``if` `(A[i] != B[i]) ` `               ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `int` `main() ` `{ ` `    ``string A = ``"gee*sforgeeks"``; ` `    ``string B = ``"geeksforgeeks"``; ` `    ``cout << doMatch(A, B); ` `    ``return` `0; ` `} `

 `// Java program for string matching with *  ` `import` `java.util.*; ` ` `  `public` `class` `GfG { ` `     `  `    ``// Function to check if the two ` `    ``// strings can be matched or not ` `    ``public` `static` `int` `doMatch(String A, String B) {  ` `     `  `        ``for` `(``int` `i = ``0``; i < A.length(); i++){ ` `       `  `            ``// if the string don't have *  ` `            ``// then character at that position  ` `            ``// must be same.  ` `            ``if` `(A.charAt(i) != ``'*'` `&& B.charAt(i) != ``'*'``){   ` `                ``if` `(A.charAt(i) != B.charAt(i))  ` `                   ``return` `0``; ` `            ``} ` `        ``} ` `   `  `        ``return` `1``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String []args){ ` `         `  `        ``String A = ``"gee*sforgeeks"``;  ` `        ``String B = ``"geeksforgeeks"``;  ` `        ``System.out.println(doMatch(A, B)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rituraj Jain `

 `# Python3 program for string  ` `# matching with * ` ` `  `def` `doMatch(A, B): ` `     `  `    ``for` `i ``in` `range``(``len``(A)): ` `         `  `        ``# if the string don't have * ` `        ``# then character t that position ` `        ``# must be same. ` `        ``if` `A[i] !``=` `'*'` `and` `B[i] !``=` `'*'``: ` `            ``if` `A[i] !``=` `B[i]: ` `                ``return` `False` `    ``return` `True` ` `  `#Driver code ` `if` `__name__``=``=``'__main__'``: ` `    ``A ``=` `"gee*sforgeeks"` `    ``B ``=` `"geeksforgeeks"` `    ``print``(``int``(doMatch(A, B))) ` ` `  `# this code is contributed by  ` `# Shashank_Sharma `

 `// C# program for string matching with ` `using` `System; ` `     `  `class` `GfG  ` `{ ` `     `  `    ``// Function to check if the two ` `    ``// strings can be matched or not ` `    ``public` `static` `int` `doMatch(String A, String B) ` `    ``{  ` `     `  `        ``for` `(``int` `i = 0; i < A.Length; i++) ` `        ``{ ` `         `  `            ``// if the string don't have *  ` `            ``// then character at that position  ` `            ``// must be same.  ` `            ``if` `(A[i] != ``'*'` `&& B[i] != ``'*'``)  ` `                ``if` `(A[i] != B[i]) ` `                    ``return` `0; ` `        ``} ` `     `  `        ``return` `1;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` `        ``String A = ``"gee*sforgeeks"``;  ` `        ``String B = ``"geeksforgeeks"``;  ` `        ``Console.WriteLine(doMatch(A, B)); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

 ` `

Output:
```1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :