If I have a sequence as follows (let's say it's an IEnumerable<T>
):
[A, B, C, D, E]
Then what's the cleanest way to compute all possible (continuous and non-continuous) subsequences of a given length? Ordering of the results in the result set isn't important, but it shouldn't include duplicates.
e.g. If I want to compute all possible subsequences of length 3 the result set would be:
[A, B, C]
[A, B, D]
[A, B, E]
[A, C, D]
[A, C, E]
[A, D, E]
[B, C, D]
[B, C, E]
[B, D, E]
[C, D, E]
For the record, the accepted answer below gave me a good starting point, and here's the code I've gone with that is updated to use some of the new .NET 3.5 extension methods:
public static IEnumerable<IEnumerable<T>> Subsequences<T>(
this IEnumerable<T> source,
int count)
{
if (count == 0)
{
yield return Enumerable.Empty<T>();
}
else
{
var skip = 1;
foreach (var first in source)
{
foreach (var rest in source.Skip(skip).Subsequences(count - 1))
{
yield return Enumerable.Repeat(first, 1).Concat(rest);
}
skip++;
}
}
}
@RossMillikan So the total number of subsequences in a string is 2n, where n is the length of the string.
The total number of subsequences possible is equal to 2^n. Hence, the time complexity is O(2^n) which is exponential. We use an unordered set to store all unique subsequences. In the worst case, the number of unique subsequences can be equal to 2^n.
A subsequence is derived from the original sequence by removing elements, keeping the order. One can remove finite or infinite many elements. Valid edge cases are: remove all elements of the original sequence.
I've had success with IanG's PermuteUtils
class:
char[] items = new char[] { 'A', 'B', 'C', 'D', 'E' };
foreach (IEnumerable<char> permutation in PermuteUtils.Permute(items, 3)) {
Console.Write("[");
foreach (char c in permutation) {
Console.Write(" " + c);
}
Console.WriteLine(" ]");
}
Results in:
[ A B C ] [ A B D ] [ A B E ] [ A C B ] [ A C D ] [ A C E ] [ A D B ] [ A D C ] [ A D E ] [ A E B ] [ A E C ] [ A E D ] [ B A C ] [ B A D ] [ B A E ] [ B C A ] [ B C D ] [ B C E ] [ B D A ] [ B D C ] ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With