📌  相关文章
📜  构造一个由给定数组指定的具有最长公共前缀的字符串数组

📅  最后修改于: 2021-10-26 06:07:41             🧑  作者: Mango

给定一个大小为N的整数数组arr[] ,任务是构造一个由N+1个长度为N 的字符串组成的数组,使得arr[i]等于i字符串的最长公共前缀和(i+1)字符串。

例子:

方法:

请按照以下步骤解决问题:

  • 我们的想法是,通过改变N字符串观察从i,如果i字符串已知,那么第(i-1)字符串可以形成– ARR [I-1]i字符的字符串。
  • 从右到左开始构造字符串并生成N+1 个字符串。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the array of strings
vector solve(int n, int arr[])
{
    // Marks the (N+1)th string
    string s = string(n, 'a');
 
    vector 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 ans = solve(n, arr);
 
    // Print the strings
    for (int i = ans.size() - 1; i >= 0; i--) {
        cout << ans[i] << endl;
    }
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to find the array of strings
  static Vector solve(int n, int arr[])
  {
     
    // Marks the (N+1)th string
    String s = "aaa";
    Vector ans = new Vector();
    ans.add(s);
 
    // To generate remaining N strings
    for (int i = n-1 ; i >= 0; i--)
    {
 
      // Check if current character
      // is b
      if(s.length() - 1 >= arr[i])
      {
 
        // Find i-th string using
        // (i+1)-th string
        char ch = s.charAt(arr[i]);
        if (ch == 'b')
          ch = 'a';
 
        // Otherwise
        else
          ch = 'b';
        char[] myNameChars =s.toCharArray();
        myNameChars[arr[i]] = ch;
        s = String.valueOf(myNameChars);
      }
 
      // Insert the string
      ans.add(s);
    }
 
    // Return the answer
    return ans;
  }
 
  // Driver Code
  public static void main(String []args)
  {
 
    int []arr = { 2, 0, 3};
    int n = arr.length;
    Vector ans = solve(n, arr);
 
    // Print the strings
    for (int i = ans.size()-1; i >= 0; i--)
    {
      System.out.println(ans.get(i));
    }
  }
}
 
// This code is contributed by bgangwar59.


Python3
# Python3 Program to implement
# the above approach
 
# Function to find the array
# of strings
def solve(n, arr):
 
    # Marks the (N+1)th
    # string
    s = 'a' * (n)
    ans = []
    ans.append(s)
 
    # To generate remaining
    # N strings
    for i in range(n - 1,
                   -1, -1):
 
        # Find i-th string using
        # (i+1)-th string   
        if len(s) - 1 >= arr[i]:
           ch = s[arr[i]]
 
           # Check if current
           # character
           # is b
           if (ch == 'b'):
               ch = 'a'
 
           # Otherwise
           else:
               ch = 'b'
            
           p = list(s)
           p[arr[i]] = ch
           s = ''.join(p)
 
        # Insert the string
        ans.append(s)
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    arr = [2, 0, 3]
    n = len(arr)
    ans = solve(n, arr)
 
    # Print the strings
    for i in range(len(ans) - 1,
                   -1, -1):
        print(ans[i])
 
# This code is contributed by Chitranayal


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to find the array of strings
  static List solve(int n, int []arr)
  {
 
    // Marks the (N+1)th string
    string s = "aaa";
 
    List ans = new List();
    ans.Add(s);
 
    // To generate remaining N strings
    for (int i = n - 1; i >= 0; i--) {
 
      // Find i-th string using
      // (i+1)-th string
      if(s.Length-1>=arr[i]){
        char ch = s[arr[i]];
 
        // Check if current character
        // is b
        if (ch == 'b')
          ch = 'a';
 
        // Otherwise
        else
          ch = 'b';
        char[] chr = s.ToCharArray();
        chr[arr[i]] = ch;
        s =  new string(chr);
      }
 
      // Insert the string
      ans.Add(s);
    }
 
    // Return the answer
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
 
    int []arr = { 2, 0, 3 };
    int n = arr.Length;
    List ans = solve(n, arr);
 
    // Print the strings
    for (int i = ans.Count - 1; i >= 0; i--) {
      Console.WriteLine(ans[i]);
    }
  }
}        
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
bab
baa
aaa
aaa

时间复杂度: O(N)
辅助空间: O(N)