Last active
February 19, 2021 17:33
-
-
Save mick-io/a7f0e3e2b440f621aa975a9a9aa7259e to your computer and use it in GitHub Desktop.
String Permutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
permutations := createPermutations("abc") | |
fmt.Println(permutations) | |
} | |
func createPermutations(s string) (permutations []string) { | |
if len(s) < 2 { | |
return []string{s} | |
} | |
for i, r := range s { | |
remainder := s[0:i] + s[i+1:] | |
for _, chars := range createPermutations(remainder) { | |
permutations = append(permutations, string(r)+chars) | |
} | |
} | |
return | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I don't know Java, show me a better way. | |
import java.util.ArrayList; | |
class Sandbox { | |
public static void main(String[] args) { | |
ArrayList<String> permutations = createPermutations("abc"); | |
System.out.println(permutations.toString()); | |
} | |
private static ArrayList<String> createPermutations(String str) { | |
ArrayList<String> permutations = new ArrayList<String>(); | |
if (str.length() < 2) { | |
permutations.add(str); | |
return permutations; | |
} | |
char[] characters = str.toCharArray(); | |
for (int i = 0; i < characters.length; i++) { | |
char ch = characters[i]; | |
StringBuilder remainder = new StringBuilder(); | |
for (int j = 0; j < characters.length; j++) { | |
if (i != j) { | |
remainder.append(characters[j]); | |
} | |
} | |
for (String chars : createPermutations(remainder.toString())) { | |
permutations.add(ch + chars); | |
} | |
} | |
return permutations; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
function createPermutations(str) { | |
if (str.length < 2) { | |
return [str]; | |
} | |
const permutations = []; | |
for (let i = 0, len = str.length; i < len; i++) { | |
let char = str.charAt(i); | |
let remaining = str.slice(0, i) + str.slice(i + 1, len); | |
for (const permutation of createPermutations(remaining)) { | |
permutations.push(char + permutation); | |
} | |
} | |
return permutations; | |
} | |
const permutations = createPermutations("abc"); | |
console.log(permutations); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# I don't know Ruby, show me a better way. | |
def create_permuations(s) | |
return [s] if s.length < 2 | |
permutations = [] | |
s.split("").each_with_index do |ch, i| | |
remainder = s[0...i] + s[i+1..-1] | |
create_permuations(remainder).each do |s| | |
permutations.append(ch + s) | |
end | |
end | |
return permutations | |
end | |
if __FILE__ == $0 | |
output = create_permuations("abc") | |
puts output | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from typing import List | |
def create_permutations(string: str) -> List[str]: | |
if len(string) < 2: | |
return [string] | |
perumations = [] | |
for i, c in enumerate(string): | |
remainder = string[0:i] + string[i+1:] | |
for chars in create_permutations(remainder): | |
perumations.append(c + chars) | |
return perumations | |
if __name__ == "__main__": | |
permutation = create_permutations("abc") | |
print(permutation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment