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:
"qwertyuiop"
,"asdfghjkl"
, and"zxcvbnm"
.Example 1:
Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]
Explanation:
Both "a"
and "A"
are in the 2nd row of the American keyboard due to case insensitivity.
Example 2:
Input: words = ["omk"]
Output: []
Example 3:
Input: words = ["adsdf","sfd"]
Output: ["adsdf","sfd"]
Constraints:
1 <= words.length <= 20
1 <= words[i].length <= 100
words[i]
consists of English letters (both lowercase and uppercase). When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem is to check each word one by one against each of the keyboard rows. We determine which words can be typed using only one keyboard row by comparing the letters within the word against the letters in each row.
Here's how the algorithm would work step-by-step:
def find_words_from_keyboard_row(words):
top_row = set('qwertyuiop')
middle_row = set('asdfghjkl')
bottom_row = set('zxcvbnm')
result_words = []
for word in words:
word_lower = word.lower()
# Check if the word can be typed using the top row
if all(letter in top_row for letter in word_lower):
result_words.append(word)
# Check if the word can be typed using the middle row
elif all(letter in middle_row for letter in word_lower):
result_words.append(word)
# Check if the word can be typed using the bottom row
elif all(letter in bottom_row for letter in word_lower):
result_words.append(word)
return result_words
The key idea is to figure out which keyboard row each letter belongs to and then check if all letters of a word are on the same row. We can do this efficiently by using a simple way to quickly look up the row for each letter, rather than searching through rows repeatedly.
Here's how the algorithm would work step-by-step:
def find_words_from_keyboard_row(words):
first_row = set('qwertyuiopQWERTYUIOP')
second_row = set('asdfghjklASDFGHJKL')
third_row = set('zxcvbnmZXCVBNM')
row_map = {}
for letter in first_row:
row_map[letter] = 1
for letter in second_row:
row_map[letter] = 2
for letter in third_row:
row_map[letter] = 3
result = []
for word in words:
#Determine which row the first letter exists on
if not word:
continue
first_letter = word[0]
row_number = row_map[first_letter]
is_valid = True
#Verify that all letters are in the same row
for letter in word:
if row_map[letter] != row_number:
is_valid = False
break
if is_valid:
result.append(word)
return result
Case | How to Handle |
---|---|
Empty input array of words | Return an empty list since there are no words to check. |
Input array containing an empty string | Treat empty strings as not typeable on a single row and exclude them from the output. |
Words with mixed-case letters | Convert each word to lowercase before checking to ensure case-insensitivity. |
Words containing non-alphabetic characters (numbers, symbols, spaces) | Filter out such words or throw an error, as the problem description only mentions alphabetic characters. |
Words with a length of 1 | Words with only one character are always typeable on one row, so include them if applicable. |
Words where the first character determines the row, but subsequent characters are from a different row | The solution should correctly identify that the word cannot be typed on a single row. |
All words in the input can be typed on one row | The solution should return the original array as is since all words satisfy the condition. |
No words in the input can be typed on one row | The solution should return an empty list since no words satisfy the condition. |