Check if a given pattern exists in a given string or not including wild cards * and .


Check if a given pattern exists in a given string or not including wild cards * and . is a common problem in computer science and programming. In this problem, we are given a string (text) and a pattern, which can contain wildcard characters '*' and '.', and we need to check if the pattern matches the text or not. This problem is encountered in a wide range of applications, such as search engines, file systems, and network protocols.

In this tutorial, we will discuss a simple and efficient solution to this problem using C++. We will start by explaining the problem statement and its constraints, and then we will give a step-by-step guide to solving it. We will also provide a sample C++ code that implements our solution and discuss its time and space complexity. So let’s dive in!

Problem Statement

The objective is to determine whether a given pattern, containing the wildcard characters '*' and '•', matches a given text string. The lengths of the pattern and text are M and N, respectively. If the pattern matches the text, output "Yes". Otherwise, output "No".

It should be noted that the '*' character matches zero or more occurrences of the character immediately preceding it, while the '•' character matches any single character.

Sample Example 1

Input

Text: "hello world"; Pattern: "h*llo w•rld"

Output

Yes

Explanation: In this example, the text is "hello world" and the pattern is "hllo w•rld". The '' character in the pattern matches zero or more occurrences of the character immediately preceding it. Therefore, it matches the 'h' in the text. The '•' character matches any single character, so it matches the space character in the text. The pattern also matches the 'o' and 'w' characters in the text. Thus, the pattern matches the text, and the output is "Yes".

Sample Example 2

Input

Text: "cat"; Pattern: "c*t•"

Output

No

Explanation: In this example, the text is "cat" and the pattern is "ct•". The '' character in the pattern matches zero or more occurrences of the character immediately preceding it. Therefore, it matches the 'c' in the text. The '•' character matches any single character, so it matches the 'a' character in the text. However, the pattern does not match the final 't' character in the text, as there is no '•' character to match it. Thus, the pattern does not match the text, and the output is "No".

Algorithm

1. Initialize a matrix, dp, with dimensions (n+1) x (m+1), where dp[i][j] represents whether the substring of the text up to index i matches the substring of the pattern up to index j.

2. Set dp[0][0] to true, as an empty text and an empty pattern always match.

3. Iterate over each character in the pattern from index 1 to m:

  • If the current character is '', set dp[0][i] to dp[0][i-1], indicating that '' can match an empty substring.

4. Iterate over each character in the text from index 1 to n:

  • For each character in the pattern from index 1 to m:

    • If the characters at the current positions in both text and pattern are the same, or the pattern character is '?', set dp[i][j] to dp[i-1][j-1], as the current characters match.

    • If the pattern character is '', set dp[i][j] to dp[i][j-1] (indicating '' matches an empty substring) or dp[i-1][j] (indicating '*' matches the current text character).

    • Otherwise, set dp[i][j] to false.

5. The final result is stored in dp[n][m], which indicates whether the entire text matches the entire pattern.

This algorithm uses the dynamic programming approach to build the matrix, considering three possible scenarios for each character in the pattern: matching a character in the text, matching a '?', or matching a '*'. The algorithm returns true if the entire text matches the entire pattern and false otherwise.

Example

Implementation of the above algorithm using C++

The below C++ program aims to determine whether a given text matches a pattern that contains wildcards represented by '*' and '?' symbols. The algorithm utilizes dynamic programming to construct a matrix, dp[n+1][m+1], where n is the length of the text and m is the length of the pattern.

Input

"hello world"; string pattern = "h*llo w?rld";

Output

Yes

Example

#include <iostream>
#include <vector>
bool isMatch(const std::string& text, const std::string& pattern) {
   int n = text.length();
   int m = pattern.length();
   std::vector<std::vector<bool>> dp(n + 1, std::vector<bool>(m + 1, false));
   dp[0][0] = true;
   for (int i = 1; i <= m; i++) {
      if (pattern[i - 1] == '*') {
         dp[0][i] = dp[0][i - 1];
      }
   }
   for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
         if (text[i - 1] == pattern[j - 1] || pattern[j - 1] == '?') {
            dp[i][j] = dp[i - 1][j - 1];
         } else if (pattern[j - 1] == '*') {
            dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
         } else {
            dp[i][j] = false;
         }
      }
   }
   return dp[n][m];
}
int main() {
   std::string text = "hello world";
   std::string pattern = "h*llo w?rld";
   if (isMatch(text, pattern)) {
      std::cout << "Yes\n";
   } else {
      std::cout << "No\n";
   }
   return 0;
}

Output

Yes

Conclusion

To sum up, the provided program implements a pattern-matching algorithm using dynamic programming. It determines whether a given text matches a pattern containing wildcards '*' and '?' symbols. By constructing a matrix and iteratively comparing characters, the algorithm efficiently evaluates the matching conditions. The unique approach ensures accurate pattern-matching results, allowing for versatile text comparisons. This program serves as a valuable tool for various applications, such as string matching, text processing, and data analysis. Its efficiency and reliability make it a reliable choice when dealing with pattern-matching tasks.

Updated on: 08-Sep-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements