diff --git a/data_structures/arrays/Permutations.swift b/data_structures/arrays/Permutations.swift new file mode 100644 index 0000000..6b14e37 --- /dev/null +++ b/data_structures/arrays/Permutations.swift @@ -0,0 +1,53 @@ +// +// Permutations.swift +// +// +// Created by Moon Jongseek on 11/22/24. +// + +/// Returns the array of permutation with the specified length. +/// +/// Generates permutations in ascending order based on the index. +/// The following example shows how to generate permutaions for the `Int` type: +/// +/// let permutationSet = permutations(data: [1,2,3,4], count: 2) +/// // [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]] +/// +/// - Parameters: +/// - data: The array data to generate permutations. +/// - count: The number of each permutation elements. +/// +/// - Returns: An array of permutations with length `count`. if `count` is greater than the length of `data` or less than 0, returns `nil`. +/// +/// - Complexity: O(n! * n) +/// +func permutations(data: [T], count: Int) -> [[T]]? { + guard data.count >= count && count >= 0 else { return nil } + var result: [[T]] = [] + var buffer: [T] = [] + var visited = Array(repeating: false, count: data.count) + + func nextElement() { + if buffer.count == count { + result.append(buffer) + return + } + for i in 0..