Keyboard Row - Problem
Keyboard Row Challenge
Imagine you're typing on a traditional QWERTY keyboard and want to find words that can be typed using letters from only one row of the keyboard. This is a common typing optimization problem!
Given an array of strings
The American keyboard has three rows:
• First row:
• Second row:
• Third row:
Note: The comparison is case-insensitive - both 'A' and 'a' are treated as the same letter from the second row.
Goal: Return an array containing only the words that can be typed using keys from a single keyboard row.
Imagine you're typing on a traditional QWERTY keyboard and want to find words that can be typed using letters from only one row of the keyboard. This is a common typing optimization problem!
Given an array of strings
words, your task is to return all words that can be typed using letters from only one row of the American keyboard.The American keyboard has three rows:
• First row:
"qwertyuiop"• Second row:
"asdfghjkl"• Third row:
"zxcvbnm"Note: The comparison is case-insensitive - both 'A' and 'a' are treated as the same letter from the second row.
Goal: Return an array containing only the words that can be typed using keys from a single keyboard row.
Input & Output
example_1.py — Basic Mixed Case
$
Input:
["Hello", "Alaska", "Dad", "Peace"]
›
Output:
["Alaska", "Dad"]
💡 Note:
"Alaska" can be typed using only the second row (asdfghjkl), and "Dad" can be typed using only the second row as well. "Hello" requires multiple rows (h from row 2, e from row 1, l from row 2, o from row 1), and "Peace" also requires multiple rows.
example_2.py — Single Characters
$
Input:
["omk"]
›
Output:
[]
💡 Note:
"omk" cannot be typed with one row: 'o' is in row 1 (qwertyuiop), 'm' is in row 3 (zxcvbnm), and 'k' is in row 2 (asdfghjkl). Since characters span across all three rows, it's not included in the result.
example_3.py — All Same Row
$
Input:
["adsdf", "sfd"]
›
Output:
["adsdf", "sfd"]
💡 Note:
Both words can be typed using only the second row (asdfghjkl). All characters 'a', 'd', 's', 'd', 'f' belong to the second row, so both words are included in the result.
Constraints
- 1 ≤ words.length ≤ 20
- 1 ≤ words[i].length ≤ 100
- words[i] consists of English letters (both lowercase and uppercase)
- Case-insensitive: 'A' and 'a' are treated as the same character
Visualization
Tap to expand
Understanding the Visualization
1
Create Zone Map
Build a directory mapping each letter to its keyboard row zone
2
Check First Letter
For each word, identify which zone the first letter belongs to
3
Verify Consistency
Ensure all remaining letters in the word belong to the same zone
4
Collect Results
Add words that pass the single-zone test to the final result
Key Takeaway
🎯 Key Insight: By creating a character-to-row mapping once, we can classify any word in O(word_length) time with O(1) space complexity, making this approach optimal for the keyboard row problem.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code