diff options
author | Jack Sangdahl <[email protected]> | 2025-09-14 18:11:52 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-14 18:11:52 -0600 |
commit | 01c6cf8af910f8e58ece342b45b22abcc8e498f4 (patch) | |
tree | 29539631cbc7e4dcba22acb74b7bfe722ff06282 | |
parent | 64b99fa5197737fab06e6e8465a203be985b07a8 (diff) | |
download | leetcode-01c6cf8af910f8e58ece342b45b22abcc8e498f4.tar.gz leetcode-01c6cf8af910f8e58ece342b45b22abcc8e498f4.zip |
1935. maximum number of words you can type
-rw-r--r-- | max_num_words_type.c | 42 | ||||
-rw-r--r-- | max_num_words_type.py | 37 |
2 files changed, 79 insertions, 0 deletions
diff --git a/max_num_words_type.c b/max_num_words_type.c new file mode 100644 index 0000000..a353e13 --- /dev/null +++ b/max_num_words_type.c @@ -0,0 +1,42 @@ +// 1935. Maximum Number of Words You Can Type +#include "leetcode.h" + +/* + * there is a malfunctioning keyboard where some letter keys do not work. all + * other keys on the keyboard work properly. given a string of words 'text' + * separated by a single space, and a string 'brokenLetters' of all distinct + * letter keys that are broken, return the number of words in 'text' you can + * fully type using this keyboard. + */ + +int mask(char *s) { + int n = strlen(s), res = 0; + for (int i = 0; i < n; i++) + res = res | (1 << (s[i] - 'a')); + return res; +} + +int canBeTypedWords(char *text, char *brokenLetters) { + int broken = mask(brokenLetters); + int n = strlen(text), ans = 0, curr = 0; + for (int i = 0; i < n; i++) { + if (text[i] == ' ') { + if (!(curr & broken)) + ans++; + curr = 0; + } else + curr = curr | (1 << (text[i] - 'a')); + } + if (!(curr & broken)) + ans++; + return ans; +} + +int main() { + char *t1 = "hello world", *bl1 = "ad"; + char *t2 = "leet code", *bl2 = "lt"; + char *t3 = "leet code", *bl3 = "e"; + printf("%d\n", canBeTypedWords(t1, bl1)); // expect: 1 + printf("%d\n", canBeTypedWords(t2, bl2)); // expect: 1 + printf("%d\n", canBeTypedWords(t3, bl3)); // expect: 0 +} diff --git a/max_num_words_type.py b/max_num_words_type.py new file mode 100644 index 0000000..fc58fba --- /dev/null +++ b/max_num_words_type.py @@ -0,0 +1,37 @@ +# 1935. Maximum Number of Words You Can Type + +""" +there is a malfunctioning keyboard where some letter keys do not work. all +other keys on the keyboard work properly. given a string of words 'text' +separated by a single space, and a string 'brokenLetters' of all distinct +letter keys that are broken, return the number of words in 'text' you can +fully type using this keyboard. +""" + + +class Solution(object): + def canBeTypedWords(self, text, brokenLetters): + """ + :type text: str + :type brokenLetters: str + :rtype: int + """ + text = text.split() + n = len(text) + brokenLetters = list(brokenLetters) + for i in text: + tmp = 0 + for j in i: + if j in brokenLetters: + tmp -= 1 + break + if tmp < 0: + n -= 1 + return n + + +if __name__ == "__main__": + obj = Solution() + print(obj.canBeTypedWords(text="hello world", brokenLetters="ad")) + print(obj.canBeTypedWords(text="leet code", brokenLetters="lt")) + print(obj.canBeTypedWords(text="leet code", brokenLetters="e")) |