Advertisement
dim4o

Words

Apr 23rd, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2.  
  3. class Words
  4. {
  5.     private static int count;
  6.  
  7.     static void Main()
  8.     {
  9.         var arr = Console.ReadLine().ToCharArray();
  10.         Array.Sort(arr);
  11.         PermuteRep(arr, 0, arr.Length - 1);
  12.         Console.WriteLine(count);
  13.     }
  14.  
  15.  
  16.     private static bool HasConsecutiveLetter(char[] word)
  17.     {
  18.         for (int i = 0; i < word.Length - 1; i++)
  19.         {
  20.             if (word[i].Equals(word[i + 1]))
  21.             {
  22.                 return true;
  23.             }
  24.         }
  25.         return false;
  26.     }
  27.  
  28.     static void PermuteRep(char[] arr, int start, int end)
  29.     {
  30.         if (!HasConsecutiveLetter(arr))
  31.         {
  32.             count++;
  33.         }
  34.  
  35.         for (int left = end - 1; left >= start; left--)
  36.         {
  37.             for (int right = left + 1; right <= end; right++)
  38.             {
  39.                 if (arr[left] != arr[right])
  40.                 {
  41.                     Swap(ref arr[left], ref arr[right]);
  42.                     PermuteRep(arr, left + 1, end);
  43.                 }
  44.             }
  45.  
  46.             var firstElement = arr[left];
  47.             for (int i = left; i <= end - 1; i++)
  48.             {
  49.                 arr[i] = arr[i + 1];
  50.             }
  51.             arr[end] = firstElement;
  52.         }
  53.     }
  54.  
  55.     static void Swap<T>(ref T first, ref T second)
  56.     {
  57.         T oldFirst = first;
  58.         first = second;
  59.         second = oldFirst;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement