Longest string which is prefix string of at least two strings
Last Updated :
23 Feb, 2023
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
Approach:
- Starting from the 0th position, iterate over every character and check if that character occurs in at least two of the strings at the current position or not.
- If it occurs, then recursively call on the next position. Otherwise,
- Update the max value by taking the current maximum with Current_position - 1.
- Finally, return the max value.
C++
// C++ program to find longest
// string which is prefix string
// of at least two strings
#include<bits/stdc++.h>
using namespace std;
int max1=0;
// Function to find Max length
// of the prefix
int MaxLength(vector<string> 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<string> 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<string> 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[0].size();
cout<<MaxLength(v, 0, m) + 1<<endl;
return 0;
}
Java
// Java program to find longest
// String which is prefix String
// of at least two Strings
import java.util.*;
class GFG{
static int max1 = 0;
// Function to find Max length
// of the prefix
static int MaxLength(Vector<String> 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 = (char)('a' + k);
Vector<String> v1 = new Vector<String>();
// Checking if char exists in
// current String or not
for(int j = 0; j < v.size(); j++)
{
if(v.get(j).charAt(i) == c)
{
v1.add(v.get(j));
}
}
// If atleast 2 String have
// that character
if(v1.size() >= 2)
{
// Recursive call to i+1
max1=Math.max(max1,
MaxLength(v1, i + 1, m));
}
else
{
max1=Math.max(max1, i - 1);
}
}
return max1;
}
// Driver code
public static void main(String[] args)
{
// Initialising Strings
String s1, s2, s3, s4, s5;
s1 = "abcde";
s2 = "abcsd";
s3 = "bcsdf";
s4 = "abcda";
s5 = "abced";
Vector<String> v = new Vector<String>();
// push Strings into vectors.
v.add(s1);
v.add(s2);
v.add(s3);
v.add(s4);
v.add(s5);
int m = v.get(0).length();
System.out.print(MaxLength(v, 0, m) + 1);
}
}
// This code is contributed by shikhasingrajput
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
C#
// C# program to find longest
// String which is prefix String
// of at least two Strings
using System;
using System.Collections.Generic;
class GFG{
static int max1 = 0;
// Function to find Max length
// of the prefix
static int MaxLength(List<String> 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 = (char)('a' + k);
List<String> v1 = new List<String>();
// Checking if char exists in
// current String or not
for(int j = 0; j < v.Count; j++)
{
if (v[j][i] == c)
{
v1.Add(v[j]);
}
}
// If atleast 2 String have
// that character
if (v1.Count >= 2)
{
// Recursive call to i+1
max1 = Math.Max(max1,
MaxLength(v1, i + 1, m));
}
else
{
max1 = Math.Max(max1, i - 1);
}
}
return max1;
}
// Driver code
public static void Main(String[] args)
{
// Initialising Strings
String s1, s2, s3, s4, s5;
s1 = "abcde";
s2 = "abcsd";
s3 = "bcsdf";
s4 = "abcda";
s5 = "abced";
List<String> v = new List<String>();
// push Strings into vectors.
v.Add(s1);
v.Add(s2);
v.Add(s3);
v.Add(s4);
v.Add(s5);
int m = v[0].Length;
Console.Write(MaxLength(v, 0, m) + 1);
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// Javascript program to find longest
// string which is prefix string
// of at least two strings
let max1=0;
// Function to find Max length
// of the prefix
function MaxLength(v, i, m)
{
// Base case
if(i>=m)
{
return m-1;
}
// Iterating over all the alphabets
for(let k = 0; k < 26; k++)
{
let c = String.fromCharCode('a'.charCodeAt(0) + k);
let v1 = [];
// Checking if char exists in
// current string or not
for(let j = 0; j < v.length; j++)
{
if(v[j][i] == c)
{
v1.push(v[j]);
}
}
// If atleast 2 string have
// that character
if(v1.length >= 2)
{
// Recursive call to i+1
max1 = Math.max(max1, MaxLength(v1, i+1, m));
}
else
{
max1 = Math.max(max1, i - 1);
}
}
return max1;
}
// Driver code
// Initialising strings
let s1, s2, s3, s4, s5;
s1 = "abcde";
s2 = "abcsd";
s3 = "bcsdf";
s4 = "abcda";
s5 = "abced";
let v = [];
// push strings into vectors.
v.push(s1);
v.push(s2);
v.push(s3);
v.push(s4);
v.push(s5);
let m = v[0].length;
document.write(MaxLength(v, 0, m) + 1 + "<br>");
</script>
Time Complexity: O(N*26)
Auxiliary Space: O(1)
Similar Reads
Largest substring of str2 which is a prefix of str1 Given two string str1 and str2, the task is to find the longest prefix of str1 which is present as a substring of the string str2. Print the prefix if possible else print -1.Examples: Input: str1 = "geeksfor", str2 = "forgeeks" Output: geeks All the prefixes of str1 which are present in str2 are "g"
5 min read
Remove longest prefix of the String which has duplicate substring Given a string S of length N, the task is to remove the longest prefix of the string which has at least one duplicate substring present in S. Note: The duplicate substring cannot be the prefix itself Examples: Input: S = "GeeksforGeeks"Output: "forGeeks"Explanation: The longest substring which has a
5 min read
Print the longest prefix of the given string which is also the suffix of the same string Given string str, the task is to find the longest prefix which is also the suffix of the given string. The prefix and suffix should not overlap. If no such prefix exists then print -1. Examples: Input: str = "aabcdaabc" Output: aabc The string "aabc" is the longest prefix which is also suffix. Input
8 min read
Find length of longest subsequence of one string which is substring of another string Given two strings X and Y. The task is to find the length of the longest subsequence of string X which is a substring in sequence Y.Examples: Input : X = "ABCD", Y = "BACDBDCD"Output : 3Explanation: "ACD" is longest subsequence of X which is substring of Y.Input : X = "A", Y = "A"Output : 1Perquisit
15+ min read
Find the Longest Non-Prefix-Suffix Substring in the Given String Given a string s of length n. The task is to determine the longest substring t such that t is neither the prefix nor the suffix of string s, and that substring must appear as both prefix and suffix of the string s. If no such string exists, print -1. Example: Input: s = "fixprefixsuffix"Output: fix
7 min read
Length of longest prefix anagram which are common in given two strings Given two strings str1 and str2 of the lengths of N and M respectively, the task is to find the length of the longest anagram string that is prefix substring of both strings. Examples: Input: str1 = "abaabcdezzwer", str2 = "caaabbttyh"Output: 6Explanation: Prefixes of length 1 of string str1 and str
8 min read