forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.cs
More file actions
33 lines (27 loc) · 756 Bytes
/
Permutation.cs
File metadata and controls
33 lines (27 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Strings;
public static class Permutation
{
/// <summary>
/// Returns every anagram of a given word.
/// </summary>
/// <returns>List of anagrams.</returns>
public static List<string> GetEveryUniquePermutation(string word)
{
if (word.Length < 2)
{
return new List<string>
{
word,
};
}
var result = new HashSet<string>();
for (var i = 0; i < word.Length; i++)
{
var temp = GetEveryUniquePermutation(word.Remove(i, 1));
result.UnionWith(temp.Select(subPerm => word[i] + subPerm));
}
return result.ToList();
}
}