leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit c145fbb0137e61d512e38acab0a9444b43943647
parent 935681b3ddd0cd9c2dc0a8b7a220969e270f3829
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sun, 31 Aug 2025 08:24:57 -0600

500. keyboard row

Diffstat:
Akeyboard_row.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Akeyboard_row.py | 32++++++++++++++++++++++++++++++++
2 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/keyboard_row.c b/keyboard_row.c @@ -0,0 +1,50 @@ +// 500. Keyboard Row +#include "leetcode.h" + +/* + * 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. + */ + +char **findWords(char **words, int wordsSize, int *returnSize) { + char **ans = (char **)malloc(wordsSize * sizeof(char *)); + int idx = 0, lut[26] = {2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, + 3, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3}; + bool same_line = true; + for (int i = 0; i < wordsSize; i++) { + same_line = true; + for (int j = 0; j < strlen(words[i]); j++) { + if (lut[(words[i][j] - 'A') % 32] != lut[(words[i][j + 1] - 'A') % 32]) { + same_line = false; + break; + } + } + if (same_line) + ans[idx++] = words[i]; + } + *returnSize = idx; + return ans; +} + +int main() { + char *w1[] = {"Hello", "Alaska", "Dad", "Peace"}, *w2[] = {"omk"}, + *w3[] = {"adsdf", "sfd"}; + int rs1, rs2, rs3; + char **fw1 = findWords((char **)w1, ARRAY_SIZE(w1), &rs1); + char **fw2 = findWords((char **)w2, ARRAY_SIZE(w2), &rs2); + char **fw3 = findWords((char **)w3, ARRAY_SIZE(w3), &rs3); + for (int i = 0; i < rs1; i++) + printf("%s ", fw1[i]); // expect: Alaska Dad + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%s ", fw2[i]); // expect: omk + printf("\n"); + for (int i = 0; i < rs3; i++) + printf("%s ", fw3[i]); // expect: adsdf sfd + printf("\n"); + free(fw1); + free(fw2); + free(fw3); +} diff --git a/keyboard_row.py b/keyboard_row.py @@ -0,0 +1,32 @@ +# 500. 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 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. +""" + + +class Solution(object): + def findWords(self, words): + """ + :type words: List[str] + :rtype: List[str] + """ + l1 = set("qwertyuiop") + l2 = set("asdfghjkl") + l3 = set("zxcvbnm") + ans = [] + for i in words: + curr = set(i.lower()) + if curr <= l1 or curr <= l2 or curr <= l3: + ans.append(i) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.findWords(words=["Hello", "Alaska", "Dad", "Peace"])) + print(obj.findWords(words=["omk"])) + print(obj.findWords(words=["adsdf", "sfd"]))