Given an integer array **arr[]** of size **N**, the task is to construct an array consisting of **N+1** strings of length **N** such that** arr[i]** is equal to the Longest Common Prefix of **i ^{th}** String and

**(i+1)**String.

^{th}**Examples:**

Input:arr[] = {1, 2, 3}

Output:{“abb”, “aab”, “aaa”, “aaa”}

Explanation:

Strings “abb” and “aab” have a single character “a” as Longest Common Prefix.

Strings “aab” and “aaa” have “aa” as Longest Common Prefix.

Strings “aaa” and “aaa” have “aa” as Longest Common Prefix.

Input:arr[]={2, 0, 3}

Output:{“bab”, “baa”, “aaa”, “aaa”}

Explanation:

Strings “bab” and “baa” have “ba” as Longest Common Prefix.

Strings “baa” and “aaa” have no common prefix.

Strings “aaa” and “aaa” have “aaa” as Longest Common Prefix.

**Approach:**

Follow the steps below to solve the problem:

- The idea is to observe that if
**i**string is known then^{th}**(i-1)**string can be formed from^{th}**i**string by changing^{th}**N – arr[i-1]**characters from**i**string.^{th} - Start constructing strings from right to left and generate the
**N + 1**strings.

**Illustration:**

N = 3, arr[] = {2, 0, 3}

Let the (N + 1)^{th}string is “aaa”

Therefore, the remaining strings from right to left are {“aaa”, “baa”, “bab”}

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 find the array of strings ` `vector<string> solve(` `int` `n, ` `int` `arr[]) ` `{ ` ` ` `// Marks the (N+1)th string ` ` ` `string s = string(n, ` `'a'` `); ` ` ` `vector<string> ans; ` ` ` `ans.push_back(s); ` ` ` ` ` `// To generate remaining N strings ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` ` ` `// Find i-th string using ` ` ` `// (i+1)-th string ` ` ` `char` `ch = s[arr[i]]; ` ` ` ` ` `// Check if current character ` ` ` `// is b ` ` ` `if` `(ch == ` `'b'` `) ` ` ` `ch = ` `'a'` `; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `ch = ` `'b'` `; ` ` ` `s[arr[i]] = ch; ` ` ` ` ` `// Insert the string ` ` ` `ans.push_back(s); ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 2, 0, 3 }; ` ` ` `int` `n = ` `sizeof` `arr / ` `sizeof` `arr[0]; ` ` ` `vector<string> ans = solve(n, arr); ` ` ` ` ` `// Print the strings ` ` ` `for` `(` `int` `i = ans.size() - 1; i >= 0; i--) { ` ` ` `cout << ans[i] << endl; ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

bab baa aaa aaa

**Time Complexity:** O(N)

**Auxiliary Space: **O(N)

## Recommended Posts:

- 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
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must
- 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 string which is prefix string of at least two strings
- Longest Common Substring in an Array of Strings
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Count of strings whose prefix match with the given string to a given length k
- Longest string in an array which matches with prefix of the given string
- Print all Strings from array A[] having all strings from array B[] as subsequence
- LCS (Longest Common Subsequence) of three strings
- Longest common anagram subsequence from N strings

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.