📜  两个字符串中任意一个与*匹配的字符串(与任意字符串匹配)

📅  最后修改于: 2021-05-04 09:41:23             🧑  作者: Mango

给您两个字符串A和B。字符串还包含特殊字符*。您可以将*替换为任何字母字符。最后,您必须告诉我们是否可以使两个字符串相同。

例子:

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

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

说明:我们如何解决以上问题,基本上我们有三种情况,
情况1:两个字符串在特定位置都包含*,这时我们可以用任何字符替换两个*,以使该字符串在该位置相等。
情况2:如果一个字符串具有字符,而另一个字符串在该位置具有*。因此,我们可以将*替换为其他字符串的相同字符。
情况3:如果两个字符串在该位置都有一个字符,则它们必须相同,否则我们就不能使它们相等。

C++
// 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
// 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
# 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#
// 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


PHP


输出:
1