# Longest string which is prefix string of at least two strings

Given a set of strings of the same length, we need to find the length of the longest string which is a prefix string of at least two strings.

Examples:

```Input:  ["abcde", "abcsd", "bcsdf", "abcda", "abced"]
Output: 4
Explanation:
Longest prefix string is "abcd".

Input:  ["pqrstq", "pwxyza", "abcdef", "pqrstu"]
Output: 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Starting from 0th position iterate over every character and check if that character occurs in at least two of the string at the current position or not.
• If occurs, then recursively call on next position. Otherwise,
• Update the max value by taking maximum with Current_position – 1.
• At last, return the max value.

## C++

 `// C++ program to find longest ` `// string which is prefix string ` `// of at least two strings ` `#include ` `using` `namespace` `std; ` `int` `max1=0; ` ` `  `// Function to find Max length  ` `// of the prefix ` `int` `MaxLength(vector v, ``int` `i,  ` `                                ``int` `m) ` `{ ` `    ``// Base case ` `    ``if``(i>=m) ` `    ``{ ` `        ``return` `m-1; ` `    ``} ` `     `  `    ``// Iterating over all the alphabets ` `    ``for``(``int` `k = 0; k < 26; k++) ` `    ``{ ` `        ``char` `c = ``'a'` `+ k; ` `        ``vector v1; ` `         `  `        ``// Checking if char exists in ` `        ``// current string or not ` `        ``for``(``int` `j = 0; j < v.size(); j++) ` `        ``{ ` `            ``if``(v[j][i] == c) ` `            ``{ ` `                ``v1.push_back(v[j]); ` `            ``} ` `        ``} ` `         `  `        ``// If atleast 2 string have  ` `        ``// that character ` `        ``if``(v1.size()>=2) ` `        ``{ ` `           ``// Recursive call to i+1  ` `           ``max1=max(max1, ` `                    ``MaxLength(v1, i+1, m)); ` `        ``} ` `        ``else` `        ``{ ` `            ``max1=max(max1, i - 1); ` `        ``} ` `    ``} ` `    ``return` `max1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `  ``// Initialising strings   ` `  ``string s1, s2, s3, s4, s5; ` `   `  `  ``s1 = ``"abcde"``; ` `  ``s2 = ``"abcsd"``; ` `  ``s3 = ``"bcsdf"``; ` `  ``s4 = ``"abcda"``; ` `  ``s5 = ``"abced"``; ` `      `  `  ``vector v; ` `     `  `  ``// push strings into vectors. ` `  ``v.push_back(s1); ` `  ``v.push_back(s2); ` `  ``v.push_back(s3); ` `  ``v.push_back(s4); ` `  ``v.push_back(s5); ` `     `  `  ``int` `m = v.size(); ` `     `  `  ``cout<

## Python3

 `# Python3 program to find longest ` `# string which is prefix string ` `# of at least two strings ` ` `  `max1 ``=` `0` ` `  `# Function to find max length  ` `# of the prefix ` `def` `MaxLength(v, i, m): ` `     `  `    ``global` `max1 ` `     `  `    ``# Base case ` `    ``if``(i >``=` `m): ` `        ``return` `m ``-` `1` `         `  `    ``# Iterating over all the alphabets ` `    ``for` `k ``in` `range``(``26``): ` `        ``c ``=` `chr``(``ord``(``'a'``) ``+` `k) ` `        ``v1 ``=` `[] ` `         `  `        ``# Checking if char exists in ` `        ``# current string or not ` `        ``for` `j ``in` `range``(``len``(v)): ` `            ``if``(v[j][i] ``=``=` `c): ` `                ``v1.append(v[j]) ` `         `  `        ``# If atleast 2 string have  ` `        ``# that character ` `        ``if``(``len``(v1) >``=` `2``): ` `             `  `            ``# Recursive call to i+1 ` `            ``max1 ``=` `max``(max1, MaxLength(v1, i ``+` `1``, m)) ` `        ``else``: ` `            ``max1 ``=` `max``(max1, i ``-` `1``) ` `             `  `    ``return` `max1 ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# Initialising strings  ` `    ``s1 ``=` `"abcde"` `    ``s2 ``=` `"abcsd"` `    ``s3 ``=` `"bcsdf"` `    ``s4 ``=` `"abcda"` `    ``s5 ``=` `"abced"` `    ``v ``=` `[] ` ` `  `    ``# Push strings into vectors. ` `    ``v.append(s1) ` `    ``v.append(s2) ` `    ``v.append(s3) ` `    ``v.append(s4) ` `    ``v.append(s5) ` `     `  `    ``m ``=` `len``(v[``0``]) ` `     `  `    ``print``(MaxLength(v, ``0``, m) ``+` `1``) ` ` `  `# This code is contributed by BhupendraSingh `

Output:

```4
```

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 Check out this Author's contributed articles.

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.

Improved By : bgangwar59

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.