📜  按字典顺序打印字符串的所有组合

📅  最后修改于: 2021-04-23 22:03:37             🧑  作者: Mango

给定一个字符串str,打印字典顺序字符串的所有组合。

例子:

Input: str = "ABC"
Output:
A
AB
ABC
AC
ACB
B
BA
BAC
BC
BCA
C
CA
CAB
CB
CBA

Input: ED
Output:
D
DE
E
ED

方法:使用映射计数字符串中所有字符的出现,然后使用递归可以打印所有可能的组合。将元素及其数量存储在两个不同的数组中。三个阵列被使用,其具有字符输入[]数组,计数[]阵列具有字符和结果[]是被在递归用于产生所有组合一个临时数组的计数。使用递归和回溯,可以打印所有组合。

下面是上述方法的实现。

C++
// C++ program to find all combinations
// of a string in lexicographical order
#include 
using namespace std;
  
// function to print string
void printResult(char* result, int len)
{
    for (int i = 0; i <= len; i++)
        cout << result[i];
    cout << endl;
}
  
// Method to found all combination
// of string it is based in tree
void stringCombination(char result[], char str[], int count[],
                       int level, int size, int length)
{
    // return if level is equal size of string
    if (level == size)
        return;
  
    for (int i = 0; i < length; i++) {
  
        // if occurrence of char is 0 then
        // skip the iteration of loop
        if (count[i] == 0)
            continue;
  
        // decrease the char occurrence by 1
        count[i]--;
  
        // store the char in result
        result[level] = str[i];
  
        // print the string till level
        printResult(result, level);
  
        // call the function from level +1
        stringCombination(result, str, count,
                          level + 1, size, length);
  
        // backtracking
        count[i]++;
    }
}
  
void combination(string str)
{
  
    // declare the map for store
    // each char with occurrence
    map mp;
  
    for (int i = 0; i < str.size(); i++) {
  
        if (mp.find(str[i]) != mp.end())
            mp[str[i]] = mp[str[i]] + 1;
        else
            mp[str[i]] = 1;
    }
  
    // initialize the input array
    // with all unique char
    char* input = new char[mp.size()];
  
    // initialize the count array with
    // occurrence the unique char
    int* count = new int[mp.size()];
  
    // temporary char array for store the result
    char* result = new char[str.size()];
  
    map::iterator it = mp.begin();
    int i = 0;
  
    for (it; it != mp.end(); it++) {
        // store the element of input array
        input[i] = it->first;
  
        // store the element of count array
        count[i] = it->second;
        i++;
    }
  
    // size of map(no of unique char)
    int length = mp.size();
  
    // size of original string
    int size = str.size();
  
    // call function for print string combination
    stringCombination(result, input, count,
                      0, size, length);
}
  
// Driver code
int main()
{
    string str = "ABC";
      
  
    combination(str);
  
    return 0;
}


Java
// Java program to find all combinations 
// of a string in lexicographical order
import java.util.HashMap;
  
class GFG 
{
      
    // function to print string
    static void printResult(char[] result,
                            int len)
    {
        for (int i = 0; i <= len; i++)
            System.out.print(result[i]);
        System.out.println();
    }
      
    // Method to found all combination 
    // of string it is based in tree
    static void stringCombination(char[] result, char[] str, 
                                  int[] count, int level, 
                                  int size, int length)
    {
          
        // return if level is equal size of string
        if (level == size) 
            return;
          
        for (int i = 0; i < length; i++)
        {
              
            // if occurrence of char is 0 then 
            // skip the iteration of loop
            if (count[i] == 0) 
                continue;
              
            // decrease the char occurrence by 1
            count[i]--;
              
            // store the char in result
            result[level] = str[i];
              
            // print the string till level
            printResult(result, level);
              
            // call the function from level +1
            stringCombination(result, str, count, 
                              level + 1, size, length);
              
            // backtracking
            count[i]++;
        }
    }
      
    static void combination(String str)
    {
          
        // declare the map for store 
        // each char with occurrence
        HashMap mp = new HashMap<>();
          
        for (int i = 0; i < str.length(); i++)
            mp.put(str.charAt(i), mp.get(str.charAt(i)) == null ? 1 : 
                                  mp.get(str.charAt(i)) + 1);
          
        // initialize the input array 
        // with all unique char
        char[] input = new char[mp.size()];
          
        // initialize the count array with 
        // occurrence the unique char
        int[] count = new int[mp.size()];
          
        // temporary char array for store the result
        char[] result = new char[str.length()];
          
        int i = 0;
        for (HashMap.Entry entry : mp.entrySet())
        {
              
            // store the element of input array
            input[i] = entry.getKey();
              
            // store the element of count array
            count[i] = entry.getValue();
            i++;
        }
          
        // size of map(no of unique char)
        int length = mp.size();
          
        // size of original string
        int size = str.length();
          
        // call function for print string combination
        stringCombination(result, input, count, 0, 
                                    size, length);
    }
      
    // Driver code
    public static void main (String[] args)
    {
        String str = "ABC";
        combination(str);
    }
}
  
// This code is contributed by
// sanjeev2552


C#
// C# program to find all combinations 
// of a string in lexicographical order
using System;
using System.Collections.Generic;
  
class GFG 
{
      
    // function to print string
    static void printResult(char[] result,
                            int len)
    {
        for (int i = 0; i <= len; i++)
            Console.Write(result[i]);
        Console.WriteLine();
    }
      
    // Method to found all combination 
    // of string it is based in tree
    static void stringCombination(char[] result, char[] str, 
                                int[] count, int level, 
                                int size, int length)
    {
          
        // return if level is equal size of string
        if (level == size) 
            return;
          
        for (int i = 0; i < length; i++)
        {
              
            // if occurrence of char is 0 then 
            // skip the iteration of loop
            if (count[i] == 0) 
                continue;
              
            // decrease the char occurrence by 1
            count[i]--;
              
            // store the char in result
            result[level] = str[i];
              
            // print the string till level
            printResult(result, level);
              
            // call the function from level +1
            stringCombination(result, str, count, 
                            level + 1, size, length);
              
            // backtracking
            count[i]++;
        }
    }
      
    static void combination(String str)
    {
        int i;
          
        // declare the map for store 
        // each char with occurrence
        Dictionary mp = new Dictionary();
          
        for (i= 0; i < str.Length; i++)
            if(mp.ContainsKey(str[i]))
                mp[str[i]] = mp[str[i]] + 1; 
            else
                mp.Add(str[i], 1);
          
        // initialize the input array 
        // with all unique char
        char[] input = new char[mp.Count];
          
        // initialize the count array with 
        // occurrence the unique char
        int[] count = new int[mp.Count];
          
        // temporary char array for store the result
        char[] result = new char[str.Length];
          
        i = 0;
        foreach(KeyValuePair entry in mp)
        {
              
            // store the element of input array
            input[i] = entry.Key;
              
            // store the element of count array
            count[i] = entry.Value;
            i++;
        }
          
        // size of map(no of unique char)
        int length = mp.Count;
          
        // size of original string
        int size = str.Length;
          
        // call function for print string combination
        stringCombination(result, input, count, 0, 
                                    size, length);
    }
      
    // Driver code
    public static void Main(String[] args)
    {
        String str = "ABC";
        combination(str);
    }
}
  
// This code is contributed by Rajput-Ji


输出:
A
AB
ABC
AC
ACB
B
BA
BAC
BC
BCA
C
CA
CAB
CB
CBA