Skip to content

Instantly share code, notes, and snippets.

@zoeisnowooze
Last active January 10, 2022 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoeisnowooze/a3f57a93e9d008958f88f355c49f5025 to your computer and use it in GitHub Desktop.
Save zoeisnowooze/a3f57a93e9d008958f88f355c49f5025 to your computer and use it in GitHub Desktop.
Case permutations
pub fn case_permutations(s: String) -> Vec<String> {
let mut stack: Vec<String> = vec![String::with_capacity(s.capacity())];
for c in s.chars() {
if c.is_ascii_alphabetic() {
let mut new_stack: Vec<String> = vec![];
for p in stack {
let mut lower = p.clone();
lower.push(c.to_ascii_lowercase());
new_stack.push(lower);
let mut upper = p;
upper.push(c.to_ascii_uppercase());
new_stack.push(upper);
}
stack = new_stack;
} else {
for p in stack.iter_mut() {
p.push(c);
}
}
}
stack
}
#[test]
fn test_case_permutations() {
assert_eq!(
case_permutations("ab2".to_string()),
vec!["ab2", "aB2", "Ab2", "AB2"]
);
assert_eq!(case_permutations("35p".to_string()), vec!["35p", "35P"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment