#500
Easy Algorithms Keyboard row
Array Hash Table String
73.6% acceptance
Jan 13, 2026
1813
1158
Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
Note that the strings are case-insensitive, both lowercased and uppercased of the same letter are treated as if they are at the same row.
In the American keyboard:
the first row consists of the characters "qwertyuiop",
the second row consists of the characters "asdfghjkl", and
the third row consists of the characters "zxcvbnm".
Solution
Rust
Time O(n)
Space O(n)
use std::collections::HashSet;
impl Solution {
pub fn find_words(words: Vec<String>) -> Vec<String> {
let rows = vec![
"qwertyuiop".chars().collect::<HashSet<char>>(),
"asdfghjkl".chars().collect::<HashSet<char>>(),
"zxcvbnm".chars().collect::<HashSet<char>>(),
];
words.into_iter().filter(|word| {
let word_lower = word.to_lowercase();
rows.iter().any(|row| {
word_lower.chars().all(|c| row.contains(&c))
})
}).collect()
}
}