diff options
author | Jack Sangdahl <[email protected]> | 2025-01-08 10:25:35 +0100 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-01-08 10:25:35 +0100 |
commit | f416afbe5c617eb8aeae025d0b2d73c60145dbd3 (patch) | |
tree | c5161adaa26c5f4f2a302ef129de1e89d76d99d5 | |
parent | 8a2b42fc8d607c983f5ce0f746253441fe7c38c4 (diff) | |
download | leetcode-f416afbe5c617eb8aeae025d0b2d73c60145dbd3.tar.gz leetcode-f416afbe5c617eb8aeae025d0b2d73c60145dbd3.zip |
3042. count prefix and suffix pairs 1
-rw-r--r-- | count_prefix_suffix_pair1.c | 42 | ||||
-rw-r--r-- | count_prefix_suffix_pair1.py | 34 |
2 files changed, 76 insertions, 0 deletions
diff --git a/count_prefix_suffix_pair1.c b/count_prefix_suffix_pair1.c new file mode 100644 index 0000000..e22e030 --- /dev/null +++ b/count_prefix_suffix_pair1.c @@ -0,0 +1,42 @@ +// 3042. Count Prefix and Suffix Pairs I +#include "leetcode.h" + +/* + * you are given a 0-indexed string array 'words'. let's define a boolean + * function 'isPrefixSuffix' that takes two strings 'str1' and 'str2'. + * 'isPrefixSuffix()' returns true if 'str1' is both a prefix and suffix of + * 'str2', and false otherwise. return an integer denoting the number of index + * pairs '(i, j)' such that 'i < j' and 'isPrefixSuffix(words[i], words[j])' is + * true. + */ + +bool isPrefixAndSuffix(char *str1, char *str2) { + int n = strlen(str1), m = strlen(str2); + for (int i = 0, j = 0; str1[i]; i++, j++) + if (str1[i] != str2[j]) + return false; + for (int i = n - 1, j = m - 1; j >= 0 && i >= 0; i--, j--) + if (str1[i] != str2[j]) + return false; + return true; +} + +int countPrefixSuffixPairs(char **words, int wordsSize) { + int cnt = 0; + for (int i = 0; i < wordsSize; i++) + for (int j = i + 1; j < wordsSize; j++) + if (isPrefixAndSuffix(words[i], words[j])) + cnt++; + return cnt; +} + +int main() { + char *w1[] = {"a", "aba", "ababa", "aa"}, + *w2[] = {"pa", "papa", "ma", "mama"}, *w3[] = {"abab", "ab"}; + printf("%d\n", + countPrefixSuffixPairs((char **)w1, ARRAY_SIZE(w1))); // expect: 4 + printf("%d\n", + countPrefixSuffixPairs((char **)w2, ARRAY_SIZE(w2))); // expect: 2 + printf("%d\n", + countPrefixSuffixPairs((char **)w3, ARRAY_SIZE(w3))); // expect: 0 +} diff --git a/count_prefix_suffix_pair1.py b/count_prefix_suffix_pair1.py new file mode 100644 index 0000000..7248d9b --- /dev/null +++ b/count_prefix_suffix_pair1.py @@ -0,0 +1,34 @@ +# 3042. Count Prefix and Suffix Pairs I + +""" +you are given a 0-indexed string array 'words'. let's define a boolean +function 'isPrefixSuffix' that takes two strings 'str1' and 'str2'. +'isPrefixSuffix()' returns true if 'str1' is both a prefix and suffix of +'str2', and false otherwise. return an integer denoting the number of index +pairs '(i, j)' such that 'i < j' and 'isPrefixSuffix(words[i], words[j])' is +true. +""" + + +class Solution(object): + def countPrefixSuffixPairs(self, words): + """ + :type words: List[str] + :rtype: int + """ + cnt = 0 + for i in range(len(words)): + for j in range(i + 1, len(words)): + if ( + words[j].find(words[i], 0, len(words[i])) == 0 + and words[j][::-1].find(words[i][::-1], 0, len(words[i])) == 0 + ): + cnt += 1 + return cnt + + +if __name__ == "__main__": + obj = Solution() + print(obj.countPrefixSuffixPairs(words=["a", "aba", "ababa", "aa"])) + print(obj.countPrefixSuffixPairs(words=["pa", "papa", "ma", "mama"])) + print(obj.countPrefixSuffixPairs(words=["abab", "ab"])) |