• 面试题:“中国浙江杭州”这样的一串字符串有多少个不重复的排序组合?


    “中国浙江杭州”这样的一串字符串有多少个不重复的排序组合?要求:长度不变,不能重复。

    1) java实现:

    1. import java.util.ArrayList;
    2. import java.util.List;
    3. public class StringCombination {
    4. public static void main(String[] args) {
    5. String input = "中国浙江杭州";
    6. int length = 6;
    7. List combinations = generateCombinations(input, length);
    8. System.out.println("满足条件的组合数量:" + combinations.size());
    9. System.out.println("满足条件的组合:");
    10. for (String combination : combinations) {
    11. System.out.println(combination);
    12. }
    13. }
    14. private static List generateCombinations(String input, int length) {
    15. List combinations = new ArrayList<>();
    16. generateCombinationsHelper(input, length, "", combinations);
    17. return combinations;
    18. }
    19. private static void generateCombinationsHelper(String input, int length, String currentCombination, List combinations) {
    20. if (currentCombination.length() == length) {
    21. combinations.add(currentCombination);
    22. return;
    23. }
    24. for (int i = 0; i < input.length(); i++) {
    25. char c = input.charAt(i);
    26. if (!currentCombination.contains(String.valueOf(c))) {
    27. generateCombinationsHelper(input, length, currentCombination + c, combinations);
    28. }
    29. }
    30. }
    31. }

    2)C#实现:

    1. csharp
    2. using System;
    3. using System.Collections.Generic;
    4. namespace StringCombination
    5. {
    6. class Program
    7. {
    8. static void Main(string[] args)
    9. {
    10. string input = "中国浙江杭州";
    11. int length = 6;
    12. List<string> combinations = GenerateCombinations(input, length);
    13. Console.WriteLine("满足条件的组合数量:" + combinations.Count);
    14. Console.WriteLine("满足条件的组合:");
    15. foreach (string combination in combinations)
    16. {
    17. Console.WriteLine(combination);
    18. }
    19. }
    20. private static List<string> GenerateCombinations(string input, int length)
    21. {
    22. List<string> combinations = new List<string>();
    23. GenerateCombinationsHelper(input, length, "", combinations);
    24. return combinations;
    25. }
    26. private static void GenerateCombinationsHelper(string input, int length, string currentCombination, List<string> combinations)
    27. {
    28. if (currentCombination.Length == length)
    29. {
    30. combinations.Add(currentCombination);
    31. return;
    32. }
    33. for (int i = 0; i < input.Length; i++)
    34. {
    35. char c = input[i];
    36. if (!currentCombination.Contains(c.ToString()))
    37. {
    38. GenerateCombinationsHelper(input, length, currentCombination + c, combinations);
    39. }
    40. }
    41. }
    42. }
    43. }

     3) python实现:

    1. def generate_combinations(input_string, length):
    2. combinations = []
    3. generate_combinations_helper(input_string, length, "", combinations)
    4. return combinations
    5. def generate_combinations_helper(input_string, length, current_combination, combinations):
    6. if len(current_combination) == length:
    7. combinations.append(current_combination)
    8. return
    9. for i in range(len(input_string)):
    10. char = input_string[i]
    11. if char not in current_combination:
    12. generate_combinations_helper(input_string, length, current_combination + char, combinations)
    13. input_string = "上海金燕航空"
    14. length = 6
    15. combinations = generate_combinations(input_string, length)
    16. print("满足条件的组合数量:", len(combinations))
    17. print("满足条件的组合:")
    18. for combination in combinations:
    19. print(combination)

    4) object-c实现:

    1. objective-c
    2. #import
    3. void generateCombinationsHelper(NSString *input, int length, NSString *currentCombination, NSMutableArray *combinations) {
    4. if (currentCombination.length == length) {
    5. [combinations addObject:currentCombination];
    6. return;
    7. }
    8. for (int i = 0; i < input.length; i++) {
    9. unichar c = [input characterAtIndex:i];
    10. NSString *charString = [NSString stringWithCharacters:&c length:1];
    11. if (![currentCombination containsString:charString]) {
    12. generateCombinationsHelper(input, length, [currentCombination stringByAppendingString:charString], combinations);
    13. }
    14. }
    15. }
    16. NSArray *generateCombinations(NSString *input, int length) {
    17. NSMutableArray *combinations = [NSMutableArray array];
    18. generateCombinationsHelper(input, length, @"", combinations);
    19. return combinations;
    20. }
    21. int main(int argc, const char * argv[]) {
    22. @autoreleasepool {
    23. NSString *input = @"上海金燕航空";
    24. int length = 6;
    25. NSArray *combinations = generateCombinations(input, length);
    26. NSLog(@"满足条件的组合数量:%lu", (unsigned long)combinations.count);
    27. NSLog(@"满足条件的组合:");
    28. for (NSString *combination in combinations) {
    29. NSLog(@"%@", combination);
    30. }
    31. }
    32. return 0;
    33. }

     5) C++实现:

    1. #include
    2. #include
    3. using namespace std;
    4. void generateCombinationsHelper(string input, int length, string currentCombination, vector& combinations) {
    5. if (currentCombination.length() == length) {
    6. combinations.push_back(currentCombination);
    7. return;
    8. }
    9. for (int i = 0; i < input.length(); i++) {
    10. char c = input[i];
    11. if (currentCombination.find(c) == string::npos) {
    12. generateCombinationsHelper(input, length, currentCombination + c, combinations);
    13. }
    14. }
    15. }
    16. vector generateCombinations(string input, int length) {
    17. vector combinations;
    18. generateCombinationsHelper(input, length, "", combinations);
    19. return combinations;
    20. }
    21. int main() {
    22. string input = "上海金燕航空";
    23. int length = 6;
    24. vector combinations = generateCombinations(input, length);
    25. cout << "满足条件的组合数量:" << combinations.size() << endl;
    26. cout << "满足条件的组合:" << endl;
    27. for (string combination : combinations) {
    28. cout << combination << endl;
    29. }
    30. return 0;
    31. }

     6) c实现:

    1. #include
    2. #include
    3. #include
    4. void generateCombinationsHelper(char* input, int length, char* currentCombination, char** combinations, int* count) {
    5. if (strlen(currentCombination) == length) {
    6. combinations[*count] = malloc((length + 1) * sizeof(char));
    7. strcpy(combinations[*count], currentCombination);
    8. (*count)++;
    9. return;
    10. }
    11. for (int i = 0; i < strlen(input); i++) {
    12. char c = input[i];
    13. if (strchr(currentCombination, c) == NULL) {
    14. char* newCombination = malloc((strlen(currentCombination) + 2) * sizeof(char));
    15. strcpy(newCombination, currentCombination);
    16. newCombination[strlen(newCombination)] = c;
    17. newCombination[strlen(newCombination) + 1] = '\0';
    18. generateCombinationsHelper(input, length, newCombination, combinations, count);
    19. free(newCombination);
    20. }
    21. }
    22. }
    23. char** generateCombinations(char* input, int length, int* count) {
    24. char** combinations = malloc(10000 * sizeof(char*));
    25. *count = 0;
    26. generateCombinationsHelper(input, length, "", combinations, count);
    27. return combinations;
    28. }
    29. int main() {
    30. char* input = "上海金燕航空";
    31. int length = 6;
    32. int count;
    33. char** combinations = generateCombinations(input, length, &count);
    34. printf("满足条件的组合数量:%d\n", count);
    35. printf("满足条件的组合:\n");
    36. for (int i = 0; i < count; i++) {
    37. printf("%s\n", combinations[i]);
    38. free(combinations[i]);
    39. }
    40. free(combinations);
    41. return 0;
    42. }


     

  • 相关阅读:
    C语言快速入门之内存函数的使用和模拟实现
    JWFD开源工作流大模型设计器
    k8s中如何使用gpu、gpu资源讲解、nvidia gpu驱动安装
    python解析命令行参数为dict
    【imessage苹果群发苹果推】软件安装Apple证书,服务或其他方式建立任何涵盖的产品或其他代码或程序
    Github每日精选(第33期):Screenshot-to-code训练 AI 将设计模型转换为 HTML 和 CSS
    jQuery 遍历-后代深入解析分析【前端jQuery框架】
    Python之导出项目所需要的依赖库,在线或离线安装
    网关中间件-Nginx(一)
    【PID优化】基于matlab simulink正余弦算法PID优化设计【含Matlab源码 2233期】
  • 原文地址:https://blog.csdn.net/tiantian1980/article/details/134081451