I’m doing an online class learning c# and the task i have been assigned is to make a program that give you the maximum possible number of anagrams without repeating from a string of letters. The code i made works for the examples like abc , aabc, but when a large string is entered for example abcdefghijklmnoqprstuvwxyzabcdefghijklmnoqprstuvwxyz (alphabet twice) which is 52!, the result is 8.065829222532113E+26, where the result that the platform accepts is 1.20190046982384E+60. I am thinking that Aparitii function formula is not ok but i have no ideea how to make it work properly. It should be n1!n2!..nk! with n being the number of times a letter apeared and k the number of distict letters.
EDIT: the Aparitii function should find the factorial of the total distinct letters, for example: if a character is already present it should increment its factorial so at the end instead of aabc being factorial of 4! it should be 4! / 2!1!1!
using System; public class anagram { static void Main(string[] args) { string str = Console.ReadLine(); double rez1 = NrTotalLit(str); double rez2 = Aparitii(str); Console.WriteLine(rez1 / rez2); Console.ReadKey(); } static double NrTotalLit(string str) { double x = 1; for (int i = 1; i <= str.Length; i++) { x *= i; } return x; } static double Aparitii(string str) { double y = 1; double ap1 = 1; for (int i = 0; i < str.Length; i++) { for (int j = i + 1; j < str.Length; j++) { if (str[i] == str[j]) { y = (ap1 *= j) + j; // this formula might not be good } } } return y ; } }
Recent Comments