Find a string which matches all the patterns in the given array

Given an array of strings arr[] which contains patterns of characters and "*" denoting any set of characters including empty string. The task is to find a string that matches all the patterns in the array.

Note: If there is no such possible pattern, print -1.

Input: arr[] = {“pq*du*q”, “pq*abc*q”, “p*d*q”}
Output: pqduabcdq
Pattern “pqduabcdq” matches all the strings:
String 1:
First “*” can be replaced by a empty string.
Second “*” can be replaced by “abcdq”.
String 2:
First “*” can be replaced by “du”
Second “*” can be replaced by “d”
String 3:
First “*” can be replaced by “q”
Second “*” can be replaced by “uabcd”

Input: arr[] = {“a*c”, “*”}
Output: ac

Approach: The idea is to find the common prefix and suffix string in all the patterns and then the middle of every pattern can be replaced by the first or last “*” in every pattern. Below is the illustration of the approach:

  • Create three empty strings that match with the prefix, suffix and middle portion of every pattern.
  • Iterate over every pattern in the array, For every pattern:
    • Find the first and last “*” in the pattern.
    • Iterate over the existing prefix and check that if it matches with the current prefix of the pattern, If any character doesn’t match with the pattern, return -1.
    • If there is some portion which is leftover in the prefix of the current pattern, then append it to the prefix of the common string.
    • Similarly, match the suffix of the pattern.
    • Finally append all the middle characters of the string except the “*” in the middle initialized string for the common string.
  • Finally, concatenate the portions of the common string that is the prefix, middle and the suffix portion of the string.

Below is the implementation of the above approach:






// C++ implementation to find the 
// string which matches 
// all the patterns
using namespace std;
// Function to find a common string 
// which matches all the pattern
string find(vector<string> S,int N)
    // For storing prefix till
    // first most * without conflicts
    string pref;
    // For storing suffix till
    // last most * without conflicts
    string suff;
    // For storing all middle 
    // characters between 
    // first and last *
    string mid;
    // Loop to iterate over every 
    // pattern of the array
    for (int i = 0; i < N; i++) {
        // Index of the first "*"
        int first = int(
        // Index of Last "*"
        int last = int(
        // Iterate over the first "*"
        for (int z = 0; z < int(pref.size()) && 
                              z < first; z++) {
            if (pref[z] != S[i][z]) {
                return "*";
        // Prefix till first most *
        // without conflicts
        for (int z = int(pref.size()); 
                       z < first; z++) {
            pref += S[i][z];
        // Iterate till last 
        // most * from last
        for (int z = 0; z < int(suff.size()) &&
               int(S[i].size())-1-z > last; z++) {
            if (suff[z] != S[i][int(S[i].size())-1-z]) {
                return "*";
        // Make suffix till last 
        // most * without conflicts
        for (int z = int(suff.size()); 
         int(S[i].size())-1-z > last; z++) {
            suff += S[i][int(S[i].size())-1-z];
        // Take all middle characters 
        // in between first and last most *
        for (int z = first; z <= last; z++) {
            if (S[i][z] != '*') mid += S[i][z];
    reverse(suff.begin(), suff.end());
    return pref + mid + suff;
// Driver Code
int main() {
    int N = 3; 
    vector<string> s(N);
    // Take all 
    // the strings
    // Method for finding
    // common string
    return 0;




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.

My Personal Notes arrow_drop_up

Recommended Posts:

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.