Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /
    🧩
    0500 - Keyboard Row
    0500 - Keyboard Row
    🧩

    0500 - Keyboard Row

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/keyboard-row/

    Problem recap

    Given a list of words, return the words that can be typed using letters from only one row of an American keyboard.

    Approach

    Precompute a mapping from each letter to its keyboard row (1, 2, or 3). For each word:

    • Normalize to lowercase.
    • Look up the row of the first character.
    • Ensure every character maps to that same row.
    • If it does, keep the word.

    Complexity

    TimeComplexity:O(∑∣word∣)TimeComplexity: O(\sum |word|)TimeComplexity:O(∑∣word∣)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0500 - Keyboard Row """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def findWords(self, words: List[str]) -> List[str]:
            """Return words that can be typed using one keyboard row."""
    
            rows = {
                1: set("qwertyuiop"),
                2: set("asdfghjkl"),
                3: set("zxcvbnm"),
            }
    
            # Map letter -> row id for O(1) checks
            letter_to_row = {}
            for row_id, letters in rows.items():
                for ch in letters:
                    letter_to_row[ch] = row_id
    
            ans: List[str] = []
    
            for word in words:
                if not word:
                    continue
    
                w = word.lower()
                row = letter_to_row[w[0]]
    
                if all(letter_to_row[ch] == row for ch in w):
                    ans.append(word)
    
            return ans
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().findWords(["Hello", "Alaska", "Dad", "Peace"]))  # ["Alaska", "Dad"]
        print(Solution().findWords(["omk"]))  # []
        print(Solution().findWords(["adsdf", "sfd"]))  # ["adsdf", "sfd"]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()