File size: 2,516 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10009", "name": "LeetCode 0500: Keyboard Row"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Keyboard Row (Difficulty: Easy)\nTags: Array, Hash Table, String\n\nGiven 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.\nNote that the strings are case-insensitive, both lowercased and uppercased of the same letter are treated as if they are at the same row.\nIn the American keyboard:\n\nExample 1:\nInput: words = [\"Hello\",\"Alaska\",\"Dad\",\"Peace\"]\nOutput: [\"Alaska\",\"Dad\"]\nExplanation:\nBoth \"a\" and \"A\" are in the 2nd row of the American keyboard due to case insensitivity.\n\nExample 2:\nInput: words = [\"omk\"]\nOutput: []\n\nExample 3:\nInput: words = [\"adsdf\",\"sfd\"]\nOutput: [\"adsdf\",\"sfd\"]\n\nConstraints:\n- 1 <= words.length <= 20\n- 1 <= words[i].length <= 100\n- words[i] consists of English letters (both lowercase and uppercase)."}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(n)\n# Space: O(1)\n\nclass Solution(object):\n    def findWords(self, words):\n        \"\"\"\n        :type words: List[str]\n        :rtype: List[str]\n        \"\"\"\n        rows = [set(['q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p']),\n                set(['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']),\n                set(['z', 'x', 'c', 'v', 'b' ,'n', 'm'])]\n\n        result = []\n        for word in words:\n            k = 0\n            for i in range(len(rows)):\n                if word[0].lower() in rows[i]:\n                    k = i\n                    break\n            for c in word:\n                if c.lower() not in rows[k]:\n                    break\n            else:\n                result.append(word)\n        return result\n\n\nclass Solution2(object):\n    def findWords(self, words):\n        \"\"\"\n        :type words: List[str]\n        :rtype: List[str]\n        \"\"\"\n        keyboard_rows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']\n        single_row_words = []\n        for word in words:\n            for row in keyboard_rows:\n                if all(letter in row for letter in word.lower()):\n                    single_row_words.append(word)\n        return single_row_words\n\n"}}