Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Permutation #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions data_structures/arrays/Permutations.swift
Original file line number Diff line number Diff line change
@@ -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<T>(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..<data.count {
if !visited[i] {
visited[i] = true
buffer.append(data[i])
nextElement()
buffer.removeLast()
visited[i] = false
}
}
}
nextElement()
return result
}

let permutation_test0 = permutations(data: [1,2,3,4], count: 2)
print(permutation_test0)

let permutation_test1 = permutations(data: [1,2,3,4], count: -1)
print(permutation_test1)