📌  相关文章
📜  从给定数组生成最多 X 个字符的所有可能组合

📅  最后修改于: 2021-09-17 07:45:33             🧑  作者: Mango

给定一个由N 个字符组成的数组arr[] ,任务是生成至多X 个元素( 1 ≤ X ≤ N)的所有可能组合。

例子:

方法:可以使用动态规划方法解决给定的问题。请按照以下步骤解决问题:

  1. 生成可以使用 1 个字符创建的所有可能排列,即给定的数组arr[]
  2. 存储所有排列。
  3. 存储后,生成 2 个字符的所有可能排列并存储它们。
  4. 完成最后一步后,丢弃单个字符的所有排列。
  5. 以相同的方式迭代地计算排列,直到达到X。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to generate permutations of
// at most X elements from array arr[]
void differentFlagPermutations(int X,
                               vector arr)
{
    vector ml;
    ml = arr;
     
    for(int i = 0; i < ml.size(); i++)
    {
        cout << ml[i] << " ";
    }
 
    int count = ml.size();
   
    // Traverse all possible lengths
    for(int z = 0; z < X - 1; z++)
    {
         
        // Stores all combinations
        // of length z
        vector tmp;
           
        // Traverse the array
        for(int i = 0; i < arr.size(); i++)
        {
            for(int k = 0; k < ml.size(); k++)
            {
                if (arr[i] != ml[k])
                {
                     
                    // Generate all
                    // combinations of length z
                    tmp.push_back(ml[k] + arr[i]);
                    count += 1;
                }
            }
        }    
         
        // Print all combinations of length z
        for(int i = 0; i < tmp.size(); i++)
        {
            cout << tmp[i] << " ";
        }
           
        // Replace all combinations of length z - 1
        // with all combinations of length z
        ml = tmp;
    }
}
 
// Driver Code
int main()
{
     
    // Given array
    vector arr{ "c", "a", "b" };
     
    // Given X
    int X = 2;
       
    differentFlagPermutations(X, arr);
     
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to generate permutations of
// at most X elements from array arr[]
static void differentFlagPermutations(int X,
                               String[] arr)
{
    String[] ml = arr;
     
    for(int i = 0; i < ml.length; i++)
    {
        System.out.print(ml[i] + " ");
    }
 
    int count = ml.length;
   
    // Traverse all possible lengths
    for(int z = 0; z < X - 1; z++)
    {
         
        // Stores all combinations
        // of length z
        Vector tmp = new Vector();
           
        // Traverse the array
        for(int i = 0; i < arr.length; i++)
        {
            for(int k = 0; k < ml.length; k++)
            {
                if (arr[i] != ml[k])
                {
                     
                    // Generate all
                    // combinations of length z
                    tmp.add(ml[k] + arr[i]);
                    count += 1;
                }
            }
        }    
         
        // Print all combinations of length z
        for(int i = 0; i < tmp.size(); i++)
        {
            System.out.print(tmp.get(i) + " ");
        }
           
        // Replace all combinations of length z - 1
        // with all combinations of length z
        ml = tmp.toArray(new String[tmp.size()]);;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    String []arr = { "c", "a", "b" };
     
    // Given X
    int X = 2;     
    differentFlagPermutations(X, arr);  
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to generate permutations of
# at most X elements from array arr[]
def differentFlagPermutations(X, arr):
 
    ml = arr.copy()
 
    print(" ".join(ml), end =" ")
    count = len(ml)
 
    # Traverse all possible lengths
    for z in range(X-1):
         
        # Stores all combinations
        # of length z
        tmp = []
         
        # Traverse the array
        for i in arr:
            for k in ml:
                if i not in k:
                     
                    # Generate all
                    # combinations of length z
                    tmp.append(k + i)
                    count += 1
                     
        # Print all combinations of length z
        print(" ".join(tmp), end =" ")
         
        # Replace all combinations of length z - 1
        # with all combinations of length z
        ml = tmp
 
 
# Given array
arr = ['c', 'a', 'b']
 
# Given X
X = 2
 
differentFlagPermutations(X, arr)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to generate permutations of
  // at most X elements from array arr[]
  static void differentFlagPermutations(int X, List arr)
  {
    List ml = new List();
    ml = arr;
    for(int i = 0; i < ml.Count; i++)
    {
      Console.Write(ml[i] + " ");
    }
 
    int count = ml.Count;
 
    // Traverse all possible lengths
    for(int z = 0; z < X - 1; z++)
    {
 
      // Stores all combinations
      // of length z
      List tmp = new List();
 
      // Traverse the array
      for(int i = 0; i < arr.Count; i++)
      {
        for(int k = 0; k < ml.Count; k++)
        {
          if (arr[i] != ml[k])
          {
 
            // Generate all
            // combinations of length z
            tmp.Add(ml[k] + arr[i]);
            count += 1;
          }
        }
      }    
 
      // Print all combinations of length z
      for(int i = 0; i < tmp.Count; i++)
      {
        Console.Write(tmp[i] + " ");
      }
 
      // Replace all combinations of length z - 1
      // with all combinations of length z
      ml = tmp;
    }
  }
 
  // Driver code
  static void Main()
  {
     
    // Given array
    List arr = new List(new string[] { "c", "a", "b" });
 
    // Given X
    int X = 2;
 
    differentFlagPermutations(X, arr);
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出:
c a b ac bc ca ba cb ab

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程