Given an array of strings **arr[]** which contains patterns of characters and “*” denoting any set of characters including the 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. **Examples:**

Input:arr[] = {“pq*du*q”, “pq*abc*q”, “p*d*q”}Output:pqduabcdqExplanation:

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 the current prefix of the pattern, If any character doesn’t match with the pattern, return -1.
- If there is some portion that 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++

`// C++ implementation to find the ` `// string which matches ` `// all the patterns` `#include<bits/stdc++.h>` `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` `(` ` ` `S[i].find_first_of(` `'*'` `)` ` ` `);` ` ` ` ` `// Index of Last "*"` ` ` `int` `last = ` `int` `(` ` ` `S[i].find_last_of(` `'*'` `)` ` ` `);` ` ` ` ` `// 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` ` ` `s[0]=` `"pq*du*q"` `;` ` ` `s[1]=` `"pq*abc*q"` `;` ` ` `s[2]=` `"p*d*q"` `;` ` ` ` ` `// Method for finding` ` ` `// common string` ` ` `cout<<find(s,N);` ` ` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation ` `# to find the string which ` `# matches all the patterns` `# Function to find a common ` `# string which matches all ` `# the pattern` `def` `find(S, N):` ` ` `# For storing prefix ` ` ` `# till first most * ` ` ` `# without conflicts` ` ` `pref ` `=` `""` ` ` `# For storing suffix ` ` ` `# till last most * ` ` ` `# without conflicts` ` ` `suff ` `=` `""` ` ` `# For storing all middle ` ` ` `# characters between ` ` ` `# first and last *` ` ` `mid ` `=` `""` ` ` `# Loop to iterate over every ` ` ` `# pattern of the array` ` ` `for` `i ` `in` `range` `(N):` ` ` `# Index of the first "*"` ` ` `first ` `=` `int` `(S[i].index(` `"*"` `))` ` ` `# Index of Last "*"` ` ` `last ` `=` `int` `(S[i].rindex(` `"*"` `))` ` ` `# Iterate over the first "*"` ` ` `for` `z ` `in` `range` `(` `len` `(pref)):` ` ` `if` `(z < first):` ` ` `if` `(pref[z] !` `=` `S[i][z]):` ` ` `return` `"*"` ` ` `# Prefix till first most *` ` ` `# without conflicts` ` ` `for` `z ` `in` `range` `(` `len` `(pref),first):` ` ` `pref ` `+` `=` `S[i][z];` ` ` `# Iterate till last ` ` ` `# most * from last` ` ` `for` `z ` `in` `range` `(` `len` `(suff)):` ` ` `if` `(` `len` `(S[i]) ` `-` `1` `-` `z > last):` ` ` `if` `(suff[z] !` `=` `S[i][` `len` `(S[i]) ` `-` `1` `-` `z]):` ` ` `return` `"*"` ` ` `# Make suffix till last ` ` ` `# most * without conflicts` ` ` `for` `z ` `in` `range` `(` `len` `(suff), ` ` ` `len` `(S[i]) ` `-` `1` `-` `last):` ` ` `suff ` `+` `=` `S[i][` `len` `(S[i]) ` `-` `1` `-` `z]` ` ` `# Take all middle characters ` ` ` `# in between first and last most *` ` ` `for` `z ` `in` `range` `(first, last ` `+` `1` `):` ` ` `if` `(S[i][z] !` `=` `'*'` `):` ` ` `mid ` `+` `=` `S[i][z]` ` ` ` ` `suff` `=` `suff[:: ` `-` `1` `]` ` ` `return` `pref ` `+` `mid ` `+` `suff` `# Driver Code` `N ` `=` `3` `s ` `=` `["" ` `for` `i ` `in` `range` `(N)]` `# Take all ` `# the strings` `s[` `0` `] ` `=` `"pq*du*q"` `s[` `1` `] ` `=` `"pq*abc*q"` `s[` `2` `] ` `=` `"p*d*q"` `# Method for finding` `# common string` `print` `(find(s, N))` `# This code is contributed by avanitrachhadiya2155` |

*chevron_right*

*filter_none*

**Output:**

pqduabcdq

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.

## Recommended Posts:

- Longest string in an array which matches with prefix of the given string
- Given number of matches played, find number of teams in tournament
- Find all the patterns of "1(0+)1" in a given string | SET 1(General Approach)
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- String matching with * (that matches with any) in any of the two strings
- Suffix Tree Application 2 - Searching All Patterns
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Generate a string which differs by only a single character from all given strings
- Find length of longest subsequence of one string which is substring of another string
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Print the longest prefix of the given string which is also the suffix of the same string
- Length of smallest substring of a given string which contains another string as subsequence
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Find a string in lexicographic order which is in between given two strings
- Find the lexicographically smallest string which satisfies the given condition

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