Keyboard Row - Problem

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.

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"
  • The third row consists of the characters "zxcvbnm"

Input & Output

Example 1 — Mixed Rows
$ Input: words = ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
💡 Note: "Alaska" uses only row 1 letters (a,s,k), "Dad" uses only row 2 letters (d,a). "Hello" and "Peace" use multiple rows.
Example 2 — Single Character
$ Input: words = ["omk"]
Output: []
💡 Note: "omk" contains 'o' (row 1), 'm' (row 3), 'k' (row 2) - uses all three keyboard rows, so not included.
Example 3 — Case Insensitive
$ Input: words = ["adsdf", "sfd"]
Output: ["adsdf", "sfd"]
💡 Note: Both words use only row 2 letters (a,d,s,f). Case doesn't matter as stated in problem.

Constraints

  • 1 ≤ words.length ≤ 20
  • 1 ≤ words[i].length ≤ 100
  • words[i] consists of English letters.

Visualization

Tap to expand
Keyboard Row Filter: Find Single-Row WordsqwertyuiopRow 1asdfghjklRow 2zxcvbnmRow 3Input Words"Hello" → uses h,e,l,o"Alaska" → uses a,l,s,k"Dad" → uses d,a"Peace" → uses p,e,a,cValid Words"Alaska" ✓ Row 2 only"Dad" ✓ Row 2 only"Hello" ✗ Mixed rows"Peace" ✗ Mixed rowsFilter words that can be typed using only one keyboard rowOutput: ["Alaska", "Dad"]
Understanding the Visualization
1
Input
Array of words to check
2
Process
Check if each word uses only one keyboard row
3
Output
Return words that pass the single-row test
Key Takeaway
🎯 Key Insight: Pre-map letters to rows for instant validation rather than searching strings repeatedly
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
28.5K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen