Created
April 20, 2019 05:22
-
-
Save rust-play/86001cb71d2d853cf713b982c1e9f00c to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
//Is Unique: Implement an algorithm to determine if a string has all unique characters. | |
//What if you cannot use additional data structures? | |
use std::collections::HashMap; | |
fn main() { | |
//println!("Hello World!"); | |
println!("{}", has_all_unique("asdasd")); | |
println!("{}", has_all_unique_hash("asdasd")); | |
} | |
fn has_all_unique(word: &str) -> bool { | |
let mut new = word.chars().collect::<Vec<char>>(); | |
new.sort(); | |
new.dedup(); | |
new.len() == word.len() | |
} | |
fn has_all_unique_hash(word: &str) -> bool { | |
let mut hashmap = HashMap::new(); | |
for character in word.chars() { | |
if hashmap.contains_key(&character) { | |
return false; | |
} else { | |
hashmap.insert(character, 1); | |
} | |
} | |
return true; | |
} | |
#[cfg(test)] | |
mod tests_hash { | |
use super::has_all_unique_hash; | |
#[test] | |
fn test_asdasd() { | |
assert_eq!(has_all_unique_hash("asdasd"), false); | |
} | |
#[test] | |
fn test_hello() { | |
assert_eq!(has_all_unique_hash("Hello"), false); | |
} | |
#[test] | |
fn test_malayalam() { | |
assert_eq!(has_all_unique_hash("malayalam"), false); | |
} | |
#[test] | |
fn test_tim() { | |
assert_eq!(has_all_unique_hash("tim"), true); | |
} | |
#[test] | |
fn test_a() { | |
assert_eq!(has_all_unique_hash("a"), true); | |
} | |
#[test] | |
fn test_empty() { | |
assert_eq!(has_all_unique_hash(""), true); | |
} | |
#[test] | |
fn test_space() { | |
assert_eq!(has_all_unique_hash(" "), true); | |
} | |
#[test] | |
fn test_123() { | |
assert_eq!(has_all_unique_hash("123"), true); | |
} | |
#[test] | |
fn test_symbols_unique() { | |
assert_eq!(has_all_unique_hash("!#@$%^&*("), true); | |
} | |
#[test] | |
fn test_symbols_non_unique() { | |
assert_eq!(has_all_unique_hash("!##!##!"), false); | |
} | |
} | |
#[cfg(test)] | |
mod tests_sort { | |
use super::has_all_unique; | |
#[test] | |
fn test_asdasd() { | |
assert_eq!(has_all_unique("asdasd"), false); | |
} | |
#[test] | |
fn test_hello() { | |
assert_eq!(has_all_unique("Hello"), false); | |
} | |
#[test] | |
fn test_malayalam() { | |
assert_eq!(has_all_unique("malayalam"), false); | |
} | |
#[test] | |
fn test_tim() { | |
assert_eq!(has_all_unique("tim"), true); | |
} | |
#[test] | |
fn test_a() { | |
assert_eq!(has_all_unique("a"), true); | |
} | |
#[test] | |
fn test_empty() { | |
assert_eq!(has_all_unique(""), true); | |
} | |
#[test] | |
fn test_space() { | |
assert_eq!(has_all_unique(" "), true); | |
} | |
#[test] | |
fn test_123() { | |
assert_eq!(has_all_unique("123"), true); | |
} | |
#[test] | |
fn test_symbols_unique() { | |
assert_eq!(has_all_unique("!#@$%^&*("), true); | |
} | |
#[test] | |
fn test_symbols_non_unique() { | |
assert_eq!(has_all_unique("!##!##!"), false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment