Advertisement
areyesram

String Permute

Jul 12th, 2016
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace StringPermute
  6. {
  7.     static class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             var word = "abc";
  12.             foreach (var s in Permuta(string.Empty, word.ToArray()).Distinct())
  13.                 Console.WriteLine(s);
  14.         }
  15.  
  16.         private static IEnumerable<string> Permuta(string a, char[] b)
  17.         {
  18.             if (b.Length == 0)
  19.                 yield return a;
  20.             for (var i = 0; i < b.Length; i++)
  21.             {
  22.                 foreach (var s in Permuta(a + b[i], b.Where((c, j) => j != i).ToArray()))
  23.                     yield return s;
  24.             }
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement