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:3Explanation: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:2Explanation: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**(=**in string**j**)**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++

`// C++ program to implement ` `// the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to get the length ` `// of the longest common prefix ` `// by rearranging the strings ` `int` `longComPre(string arr[], ` `int` `N) ` `{ ` ` ` `// freq[i][j]: stores the frequency ` ` ` `// of a character(= j) in ` ` ` `// a string arr[i] ` ` ` `int` `freq[N][256]; ` ` ` ` ` `// Initilize freq[][] array. ` ` ` `memset` `(freq, 0, ` `sizeof` `(freq)); ` ` ` ` ` `// Traverse the given array ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `// Stores length of ` ` ` `// current string ` ` ` `int` `M = arr[i].length(); ` ` ` ` ` `// Traverse current string ` ` ` `// of the given array ` ` ` `for` `(` `int` `j = 0; j < M; ` ` ` `j++) { ` ` ` ` ` `// Update the value of ` ` ` `// freq[i][arr[i][j]] ` ` ` `freq[i][arr[i][j]]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Stores the length of ` ` ` `// longest common prefix ` ` ` `int` `maxLen = 0; ` ` ` ` ` `// Count the minimum frequency ` ` ` `// of each character in ` ` ` `// in all the strings of arr[] ` ` ` `for` `(` `int` `j = 0; j < 256; j++) { ` ` ` ` ` `// Stores minimum value ` ` ` `// in each row of freq[][] ` ` ` `int` `minRowVal = INT_MAX; ` ` ` ` ` `// Calculate minimum frequency ` ` ` `// of current character ` ` ` `// in all the strings. ` ` ` `for` `(` `int` `i = 0; i < N; ` ` ` `i++) { ` ` ` ` ` `// Update minRowVal ` ` ` `minRowVal = min(minRowVal, ` ` ` `freq[i][j]); ` ` ` `} ` ` ` ` ` `// Update maxLen ` ` ` `maxLen += minRowVal; ` ` ` `} ` ` ` `return` `maxLen; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string arr[] = { ` `"aabdc"` `, ` ` ` `"abcd"` `, ` ` ` `"aacd"` `}; ` ` ` `int` `N = 3; ` ` ` `cout << longComPre(arr, N); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N * (M + 256))**Auxiliary Space:** O(N * 256)

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:

- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Largest palindromic string possible from given strings by rearranging the characters
- Find the longest common prefix between two strings after performing swaps on second string
- Longest Common Prefix using Word by Word Matching
- Longest Common Prefix using Character by Character Matching
- Longest Common Prefix using Divide and Conquer Algorithm
- Longest Common Prefix using Binary Search
- Longest Common Prefix using Trie
- Longest Common Prefix using Sorting
- Find minimum shift for longest common prefix
- Longest Common Prefix using Linked List
- Longest Common Prefix Matching | Set-6
- Count of distinct XORs formed by rearranging two Binary strings
- Longest string which is prefix string of at least two strings
- Length of longest balanced parentheses prefix
- Longest Common Substring in an Array of Strings
- Smallest number by rearranging digits of a given number
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Longest palindromic string possible by concatenating strings from a given array

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.