How to generate all unique 2-digit permutations from a given 3-digit number in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class Program
{
	internal static ISet<string> generate_2_digit_permutations_from_3_digit_number(string threeDigitNumber) {
		ISet<string> twoDigitPermutations = new HashSet<string>();

		// Generate all two_digit_permutations of three_digit_number
		for (int i = 0; i < threeDigitNumber.Length; i++) {
			for (int j = 0; j < threeDigitNumber.Length; j++) {
				if (i != j) {
					twoDigitPermutations.Add("" + threeDigitNumber[i] + threeDigitNumber[j]);
				}
			}
		}

		return twoDigitPermutations;
	}
	public static void Main(string[] args)
	{
		string threeDigitNumber = "185";

		ISet<string> twoDigitPermutations = generate_2_digit_permutations_from_3_digit_number(threeDigitNumber);

		Console.WriteLine(string.Join(", ", twoDigitPermutations));
	}
}



/*
run:
 
18, 15, 81, 85, 51, 58
  
*/

 



answered Jun 16, 2024 by avibootz
...