> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Keyboard Row Python Solution with Tests

> Tested Python solution for LeetCode 500 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 500, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/keyboard-row/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 500   # by problem number
lcpy gen -s keyboard_row   # by problem name
```

## 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 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`.

![Keyboard](https://assets.leetcode.com/uploads/2018/10/12/keyboard.png)

### Examples

```
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.
```

```
Input: words = ["omk"]
Output: []
```

```
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).

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/keyboard_row/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/keyboard_row/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * m) where n = len(words), m = max word length
    # Space: O(1) (row sets are constant size)
    def find_words(self, words: list[str]) -> list[str]:
        rows = [set("qwertyuiop"), set("asdfghjkl"), set("zxcvbnm")]
        return [word for word in words if any(set(word.lower()) <= row for row in rows)]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* m) where n = len(words), m = max word length | O(1) (row sets are constant size) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.