diff options
author | Jack Sangdahl <[email protected]> | 2024-10-01 00:04:44 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-10-01 00:04:44 -0600 |
commit | 4d8a755bec6785b22781b4833322c8d78b99ccc7 (patch) | |
tree | 4941c1303d4c28fc43a3c979b617e34e9eea6582 /compare_strings_freq_char.py | |
parent | b8cd0d8b589f57f1c5604dcd3c0795992fbcdea0 (diff) | |
download | leetcode-4d8a755bec6785b22781b4833322c8d78b99ccc7.tar.gz leetcode-4d8a755bec6785b22781b4833322c8d78b99ccc7.zip |
1170. compare strings by frequency of the smallest character
Diffstat (limited to 'compare_strings_freq_char.py')
-rw-r--r-- | compare_strings_freq_char.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/compare_strings_freq_char.py b/compare_strings_freq_char.py new file mode 100644 index 0000000..27a19f8 --- /dev/null +++ b/compare_strings_freq_char.py @@ -0,0 +1,45 @@ +# 1170. Compare Strings by Frequency of the Smallest Character + +""" +let the function 'f(s)' be the frequency of the lexicographically smallest +character in a non empty string 's'. for example, if 's = dcce' then 'f(s) = +2' because the lexicographically smallest character is 'c', which has a +frequency of 2. you are given an array of strings 'words' and another array +of query strings 'queries'. for each query 'queries[i]', count the number of +words in 'words' such that 'f(queries[i]) < f(w)' for each 'w' in 'words'. +return an integer array 'answer' where each 'answer[i]' is the answer to the +i'th query +""" + + +class Solution(object): + def f(self, s): + t = sorted(list(s))[0] + return s.count(t) + + def numSmallerByFrequency(self, queries, words): + """ + :type queries: List[str] + :type words: List[str] + :rtype: List[int] + """ + query = [self.f(i) for i in queries] + word = [self.f(i) for i in words] + map = [] + for i in query: + cnt = 0 + for j in word: + if j > i: + cnt += 1 + map.append(cnt) + return map + + +if __name__ == "__main__": + obj = Solution() + print(obj.numSmallerByFrequency(queries=["cbd"], words=["zaaaz"])) + print( + obj.numSmallerByFrequency( + queries=["bbb", "cc"], words=["a", "aa", "aaa", "aaaa"] + ) + ) |