Length of longest common prefix possible by rearranging strings in a given array
Last Updated :
09 Aug, 2021
Given an array of strings arr[], the task is to find the length of the longest common prefix by rearranging the characters of each string of the given array.
Examples:
Input: arr[] = {“aabdc”, “abcd”, “aacd”}
Output: 3
Explanation: Rearrange characters of each string of the given array such that the array becomes {“acdab”, “acdb”, “acda”}.
Therefore, the longest common prefix of all the strings of the given array is “acd” having length equal to 3.
Input: arr[] = {“abcdef”, “adgfse”, “fhfdd”}
Output: 2
Explanation: Rearrange characters of each string of the given array such that the array becomes {“dfcaeb”, “dfgase”, “dffhd”}.
Therefore, the longest common prefix of all the strings of the given array is “df” having length equal to 2.
Naive Approach: The simplest approach to solve this problem is to generate all possible permutations of each string of the given array and find the longest common prefix of all the strings. Finally, print the length of the longest common prefix.
Time Complexity: O(N * log M * (M!)N)
Auxiliary Space: O(M), N is the number of strings, M is the length of the longest string.
Efficient Approach: To optimize the above approach the idea is to use Hashing. Follow the steps below to solve the problem:
- Initialize a 2D array, say freq[N][256] such that freq[i][j] stores the frequency of a character(= j) in string arr[i].
- Traverse the given array and stores the frequency of arr[i][j] into freq[i][arr[i][j]].
- Initialize a variable, say maxLen to store the length of the longest common prefix
- Iterate over all possible characters and find the minimum frequency, say minRowVal of the current character in all the strings of the given array, and increment the value of maxLen by minRowVal
- Finally, print the value of maxLen.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int longComPre(string arr[], int N)
{
int freq[N][256];
memset (freq, 0, sizeof (freq));
for ( int i = 0; i < N; i++) {
int M = arr[i].length();
for ( int j = 0; j < M;
j++) {
freq[i][arr[i][j]]++;
}
}
int maxLen = 0;
for ( int j = 0; j < 256; j++) {
int minRowVal = INT_MAX;
for ( int i = 0; i < N;
i++) {
minRowVal = min(minRowVal,
freq[i][j]);
}
maxLen += minRowVal;
}
return maxLen;
}
int main()
{
string arr[] = { "aabdc" ,
"abcd" ,
"aacd" };
int N = 3;
cout << longComPre(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int longComPre(String arr[],
int N)
{
int [][]freq = new int [N][ 256 ];
for ( int i = 0 ; i < N; i++)
{
int M = arr[i].length();
for ( int j = 0 ; j < M; j++)
{
freq[i][arr[i].charAt(j)]++;
}
}
int maxLen = 0 ;
for ( int j = 0 ; j < 256 ; j++)
{
int minRowVal = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++)
{
minRowVal = Math.min(minRowVal,
freq[i][j]);
}
maxLen += minRowVal;
}
return maxLen;
}
public static void main(String[] args)
{
String arr[] = { "aabdc" ,
"abcd" ,
"aacd" };
int N = 3 ;
System.out.print(longComPre(arr, N));
}
}
|
Python3
import sys
def longComPre(arr, N):
freq = [[ 0 for i in range ( 256 )]
for i in range (N)]
for i in range (N):
M = len (arr[i])
for j in range (M):
freq[i][ ord (arr[i][j])] + = 1
maxLen = 0
for j in range ( 256 ):
minRowVal = sys.maxsize
for i in range (N):
minRowVal = min (minRowVal,
freq[i][j])
maxLen + = minRowVal
return maxLen
if __name__ = = '__main__' :
arr = [ "aabdc" , "abcd" , "aacd" ]
N = 3
print (longComPre(arr, N))
|
C#
using System;
class GFG{
static int longComPre(String []arr,
int N)
{
int [,]freq = new int [N, 256];
for ( int i = 0; i < N; i++)
{
int M = arr[i].Length;
for ( int j = 0; j < M; j++)
{
freq[i, arr[i][j]]++;
}
}
int maxLen = 0;
for ( int j = 0; j < 256; j++)
{
int minRowVal = int .MaxValue;
for ( int i = 0; i < N; i++)
{
minRowVal = Math.Min(minRowVal,
freq[i, j]);
}
maxLen += minRowVal;
}
return maxLen;
}
public static void Main(String[] args)
{
String []arr = { "aabdc" ,
"abcd" ,
"aacd" };
int N = 3;
Console.Write(longComPre(arr, N));
}
}
|
Javascript
<script>
function longComPre(arr, N)
{
let freq = new Array(N);
for (let i = 0; i < N; i++)
{
freq[i] = new Array(256);
for (let j = 0; j < 256; j++)
{
freq[i][j] = 0;
}
}
for (let i = 0; i < N; i++)
{
let M = arr[i].length;
for (let j = 0; j < M; j++)
{
freq[i][arr[i][j].charCodeAt(0)]++;
}
}
let maxLen = 0;
for (let j = 0; j < 256; j++)
{
let minRowVal = Number.MAX_VALUE;
for (let i = 0; i < N; i++)
{
minRowVal = Math.min(minRowVal,
freq[i][j]);
}
maxLen += minRowVal;
}
return maxLen;
}
let arr= [ "aabdc" ,
"abcd" ,
"aacd" ];
let N = 3;
document.write(longComPre(arr, N));
</script>
|
Time Complexity: O(N * (M + 256))
Auxiliary Space: O(N * 256)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...