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
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.
C++
#include <bits/stdc++.h>
using namespace std;
bool doMatch(string A, string B)
{
for (int i = 0; i < A.length(); i++)
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
import java.util.*;
public class GfG {
public static int doMatch(String A, String B) {
for (int i = 0; i < A.length(); i++){
if (A.charAt(i) != '*' && B.charAt(i) != '*'){
if (A.charAt(i) != B.charAt(i))
return 0;
}
}
return 1;
}
public static void main(String []args){
String A = "gee*sforgeeks";
String B = "geeksforgeeks";
System.out.println(doMatch(A, B));
}
}
|
Python3
def doMatch(A, B):
for i in range(len(A)):
if A[i] != '*' and B[i] != '*':
if A[i] != B[i]:
return False
return True
if __name__=='__main__':
A = "gee*sforgeeks"
B = "geeksforgeeks"
print(int(doMatch(A, B)))
|
C#
using System;
class GfG
{
public static int doMatch(String A, String B)
{
for (int i = 0; i < A.Length; i++)
{
if (A[i] != '*' && B[i] != '*')
if (A[i] != B[i])
return 0;
}
return 1;
}
public static void Main(String []args)
{
String A = "gee*sforgeeks";
String B = "geeksforgeeks";
Console.WriteLine(doMatch(A, B));
}
}
|
PHP
<?php
function doMatch($A, $B)
{
for ($i = 0; $i < strlen($A); $i++)
if ($A[$i] != '*' && $B[$i] != '*')
if ($A[$i] != $B[$i])
return false;
return true;
}
$A = "gee*sforgeeks";
$B = "geeksforgeeks";
echo doMatch($A, $B);
?>
|
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.