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 strings contain * at a particular position, at that time we can replace both * with any character to make the string equal at that position.
- Case 2: If one string has character and the other has * at that position. So, we can replace * with the same character in another string.
- Case 3: If both strings has a character at that position, then they must be same, otherwise we can’t make them equal.
Implementation:
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));
}
}
|
Javascript
<script>
function doMatch(A, B)
{
for (i = 0; i < A.length; i++)
{
if (A.charAt(i) != '* ' && B.charAt(i) != ' *')
{
if (A.charAt(i) != B.charAt(i))
return 0;
}
}
return 1;
}
var A = "gee*sforgeeks" ;
var B = "geeksforgeeks" ;
document.write(doMatch(A, B));
</script>
|
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 );
?>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Two pointer Approach:
Steps:
- If the lengths of strings A != B, return No because it’s not possible to make them the same.
- Initialize two pointers, i=0 and j= 0, pointing to the first characters of strings A and B, respectively.
- Iterate while both pointers are within the string bounds:
1. If the characters at pointers i and j are equal or either of them is , move both pointers to the next character.
2. If the character at pointer i is , move pointer j until the characters at pointers i and j are equal or until pointer j reaches the end of string B.
3. If pointer j reaches the end of string B and pointer i is not pointing to , return No because it’s not possible to make them the same.
4. If the characters at pointers i and j are not equal and pointer i is not , return No because it’s not possible to make them the same. - If both pointers have reached the end of their respective strings, return Yes because it’s possible to make them the same.
- If either pointer i or j has not reached the end of its string, return No because it’s not possible to make them the same.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
bool isPossibleToMakeSame(string A, string B)
{
int lenA = A.length();
int lenB = B.length();
if (lenA != lenB)
return false ;
int i = 0, j = 0;
while (i < lenA && j < lenB) {
if (A[i] == B[j] || A[i] == '*' || B[j] == '*' ) {
i++;
j++;
}
else if (A[i] == '*' && B[j] != '*' ) {
while (B[j] != '*' && j < lenB)
j++;
}
else if (A[i] != '*' && B[j] == '*' ) {
while (A[i] != '*' && i < lenA)
i++;
}
else {
return false ;
}
}
if (i == lenA && j == lenB)
return true ;
else
return false ;
}
int main()
{
string A = "abs*" , B = "abds" ;
if (isPossibleToMakeSame(A, B))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
public class GFG {
public static boolean isPossibleToMakeSame(String A,
String B)
{
int lenA = A.length();
int lenB = B.length();
if (lenA != lenB)
return false ;
int i = 0 , j = 0 ;
while (i < lenA && j < lenB) {
if (A.charAt(i) == B.charAt(j)
|| A.charAt(i) == '*'
|| B.charAt(j) == '*' ) {
i++;
j++;
}
else if (A.charAt(i) == '*'
&& B.charAt(j) != '*' ) {
while (B.charAt(j) != '*' && j < lenB)
j++;
}
else if (A.charAt(i) != '*'
&& B.charAt(j) == '*' ) {
while (A.charAt(i) != '*' && i < lenA)
i++;
}
else {
return false ;
}
}
return i == lenA && j == lenB;
}
public static void main(String[] args)
{
String A = "abs*" ;
String B = "abds" ;
if (isPossibleToMakeSame(A, B))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPossibleToMakeSame(A, B):
lenA = len (A)
lenB = len (B)
if lenA ! = lenB:
return False
i = 0
j = 0
while i < lenA and j < lenB:
if A[i] = = B[j] or A[i] = = '*' or B[j] = = '*' :
i + = 1
j + = 1
elif A[i] = = '*' and B[j] ! = '*' :
while j < lenB and B[j] ! = '*' :
j + = 1
elif A[i] ! = '*' and B[j] = = '*' :
while i < lenA and A[i] ! = '*' :
i + = 1
else :
return False
if i = = lenA and j = = lenB:
return True
else :
return False
A = "abs*"
B = "abds"
if isPossibleToMakeSame(A, B):
print ( "Yes" )
else :
print ( "No" )
|
Javascript
function isPossibleToMakeSame(A, B) {
let lenA = A.length;
let lenB = B.length;
if (lenA !== lenB)
return false ;
let i = 0, j = 0;
while (i < lenA && j < lenB) {
if (A[i] === B[j] || A[i] === '*' || B[j] === '*' ) {
i++;
j++;
}
else if (A[i] === '*' && B[j] !== '*' ) {
while (B[j] !== '*' && j < lenB)
j++;
}
else if (A[i] !== '*' && B[j] === '*' ) {
while (A[i] !== '*' && i < lenA)
i++;
}
else {
return false ;
}
}
if (i === lenA && j === lenB)
return true ;
else
return false ;
}
let A = "abs*" ;
let B = "abds" ;
if (isPossibleToMakeSame(A, B))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(N), where N is the length of the input strings A and B.
Auxiliary Space: O(1)