aboutsummaryrefslogtreecommitdiff
path: root/compare_strings_freq_char.py
blob: 27a19f82d9f61344199f8e3d71f06f68ee602378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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"]
        )
    )