Construct an Array of Strings having Longest Common Prefix specified by the given Array
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 ith String and (i+1)th String.
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 ith string is known then (i-1)th string can be formed from ith string by changing N – arr[i-1] characters from ith string.
- 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++
- Java
- Python3
- C#
- Javascript
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; } |
Java
Python3
C#
Javascript
bab baa aaa aaa
Time Complexity: O(N)
Auxiliary Space: O(N)