1
Fork 0
leetcode/source/keyboard_row/mod.rs

30 lines
712 B
Rust

pub fn find_words(words: Vec<String>) -> Vec<String> {
// Uncomment FromIterator in Leetcode since they're using Rust Edition 2018.
// Edition 2021 has this trait in its prelude.
// use std::iter::FromIterator;
use std::collections::HashSet;
fn hashset_from_str(string: &str) -> HashSet<char> {
std::collections::HashSet::<_>::from_iter(string.to_lowercase().chars())
}
let rows = [
hashset_from_str("qwertyuiop"),
hashset_from_str("asdfghjkl"),
hashset_from_str("zxcvbnm"),
];
let mut result = vec![];
for word in words {
let word_set = hashset_from_str(&word);
if rows.iter().any(|row| word_set.is_subset(row)) {
result.push(word);
}
}
result
}