Keyboard Row

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of an American keyboard. The first row is "qwertyuiop", the second row is "asdfghjkl" and the third row is "zxcvbnm". Letters are case-insensitive, so "A" and "a" sit on the same row. Return the words in the order they appear in the input.

30:00

Keyboard Row
easy
Free written solution
Topics
Companies

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of an American keyboard. The first row is "qwertyuiop", the second row is "asdfghjkl" and the third row is "zxcvbnm". Letters are case-insensitive, so "A" and "a" sit on the same row. Return the words in the order they appear in the input.

Example 1:
Input: {"words":["Hello","Alaska","Dad","Peace"]}
Output: ["Alaska","Dad"]
Constraints:
  • 1≤words.length≤201 \leq \text{words.length} \leq 20

  • 1≤words[i].length≤1001 \leq \text{words}[i].\text{length} \leq 100

  • words[i]\text{words}[i] consists of English letters (both lowercase and uppercase).

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"words":["Hello","Alaska","Dad","Peace"]}

Tag every key with its row: q to p is the top row (0), a to l the middle row (1), z to m the bottom row (2). A word passes only if all its letters share one tag.

q

w

e

r

t

y

u

i

o

p

0

a

s

d

f

g

h

j

k

l

1

z

x

c

v

b

n

m

2

words (4)

Hello

Alaska

Dad

Peace

result (0)

[]

Variables
VariableValue
kept0
DepthFunction Call
Stack empty
0/23