diff options
author | Jack Sangdahl <[email protected]> | 2024-09-11 20:46:22 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-09-11 20:46:22 -0600 |
commit | 5003fa90718ed23eb1101ec320671aacef128ac2 (patch) | |
tree | 951035e643f7776d0be7f1c0a4ab4837827c227e | |
parent | 04058b633ddcf3858445bada106bbbc030cc6aac (diff) | |
download | leetcode-5003fa90718ed23eb1101ec320671aacef128ac2.tar.gz leetcode-5003fa90718ed23eb1101ec320671aacef128ac2.zip |
1684. count the number of consistent strings
-rw-r--r-- | count_num_consistent_string.c | 35 | ||||
-rw-r--r-- | count_num_consistent_string.py | 44 |
2 files changed, 79 insertions, 0 deletions
diff --git a/count_num_consistent_string.c b/count_num_consistent_string.c new file mode 100644 index 0000000..ad5ef62 --- /dev/null +++ b/count_num_consistent_string.c @@ -0,0 +1,35 @@ +// 1684. Count the Number of Consistent Strings +#include "leetcode.h" + +/* + * you are given a string 'allowed' consisting of distinct characters and an + * array of strings 'words'. a string is consistent if all characters in the + * string appear in the string 'allowed'. return the number of consistent + * strings in the array + */ + +int countConsistentStrings(char *allowed, char **words, int wordsSize) { + int s = 0, ans = 0; + for (int i = 0; allowed[i]; i++) + s |= 1 << (allowed[i] - 'a'); + for (int i = 0; i < wordsSize; i++) { + int c = 0; + for (int j = 0; words[i][j]; j++) + c |= 1 << (words[i][j] - 'a'); + if (s == (c | s)) + ans++; + } + return ans; +} + +int main() { + char *a1 = "ab", *w1[] = {"ad", "bd", "aaab", "baa", "badab"}; + char *a2 = "abc", *w2[] = {"a", "b", "c", "ab", "ac", "bc", "abc"}; + char *a3 = "cad", *w3[] = {"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"}; + printf("%d\n", + countConsistentStrings(a1, (char **)w1, ARRAY_SIZE(w1))); // expect: 2 + printf("%d\n", + countConsistentStrings(a2, (char **)w2, ARRAY_SIZE(w1))); // expect: 7 + printf("%d\n", + countConsistentStrings(a3, (char **)w3, ARRAY_SIZE(w1))); // expect: 4 +} diff --git a/count_num_consistent_string.py b/count_num_consistent_string.py new file mode 100644 index 0000000..6549fdd --- /dev/null +++ b/count_num_consistent_string.py @@ -0,0 +1,44 @@ +# 1684. Count the Number of Consistent Strings + +""" +you are given a string 'allowed' consisting of distinct characters and an +array of strings 'words'. a string is consistent if all characters in the +string appear in the string 'allowed'. return the number of consistent +strings in the array +""" + + +class Solution(object): + def countConsistentStrings(self, allowed, words): + """ + :type allowed: str + :type words: List[str] + :rtype: int + """ + allowed = set(allowed) + cnt = 0 + for w in words: + for l in w: + if l not in allowed: + cnt += 1 + break + return len(words) - cnt + + +if __name__ == "__main__": + obj = Solution() + print( + obj.countConsistentStrings( + allowed="ab", words=["ad", "bd", "aaab", "baa", "badab"] + ) + ) + print( + obj.countConsistentStrings( + allowed="abc", words=["a", "b", "c", "ab", "ac", "bc", "abc"] + ) + ) + print( + obj.countConsistentStrings( + allowed="cad", words=["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"] + ) + ) |