Function malachite_base::slices::exhaustive_slice_permutations
source · pub fn exhaustive_slice_permutations<T>(
xs: &[T],
) -> ExhaustiveSlicePermutations<'_, T> ⓘ
Expand description
Generates every permutation of a slice.
The permutations are Vec
s of references into the slice. It may be more convenient for the
iterator to own the data, in which case you may use
exhaustive_vec_permutations
instead.
The permutations are generated in lexicographic order with respect to the ordering in the slice.
The output length is $n!$, where $n$ is xs.len()
.
§Expected complexity per iteration
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is xs.len()
.
§Examples
use itertools::Itertools;
use malachite_base::slices::exhaustive_slice_permutations;
let css: Vec<String> = exhaustive_slice_permutations(&['a', 'b', 'c', 'd'])
.map(|ds| ds.into_iter().copied().collect())
.collect();
assert_eq!(
css.iter().map(String::as_str).collect_vec().as_slice(),
[
"abcd", "abdc", "acbd", "acdb", "adbc", "adcb", "bacd", "badc", "bcad", "bcda", "bdac",
"bdca", "cabd", "cadb", "cbad", "cbda", "cdab", "cdba", "dabc", "dacb", "dbac", "dbca",
"dcab", "dcba"
]
);