1705. 比较字符串 II

1705. 比较字符串 II

中文English

One string is strictly smaller than another when the frequency of occurrence of the smallest character in the string is less than the frequency of occurrence of the smallest character in the comparison string.
For example,string "abcd" is smaller than string "aaa" because the smallest character (in lexicographical order) in "abcd" is 'a', with a frequency of 1, and the smallest character in "aaa" is also 'a',but with a frequency of 3. In another example, string "a" is smaller than string "bb" beacuse the smallest character in "a" is 'a' with a frequency of 1, and the smallest character in "bb" is 'b' with a frequency of 2.
Write a function that, given string A (which contains N strings delimited by ','), returns an array C of N integers. For 0 <= J < N, values of C[J] specify the number of strings in A which are strictly smaller than the comparison J-th string in B (starting from 0).

样例

Example 1:

Input: 
A = "abcd,aabc,bd"
B = "aaa,aa"
Output: [3, 2]	
Explanation: All the strings in the array are strictly smaller than "aaa" on the basis of the given comparison cirteria;
Strings "abcd" and "bd" are strictly smaller than "aa".

注意事项

  • 1 <= N, M <= 10000
  • 1 <= length of any string contained by A or B <= 10
class Solution:
    """
    @param A: a string
    @param B: a string
    @return: returns an array C of N integers
    """
    def compareStringii(self, A, B):
        # write your code here
        if not A or not B: return [] 

        results = []
        A_array, B_array = [val for val in A.split(',')], [val for val in B.split(',')]
        A_count, B_count = [], []

        for val in A_array:
            temp_array = [v for v in val]
            A_count.append(temp_array.count(min(temp_array)))

        for val in B_array:
            temp_array = [v for v in val]
            B_count.append(temp_array.count(min(temp_array)))

        for j in B_count:
            count = 0

            for i in A_count:
                if i < j:
                    count += 1 
            results.append(count)

        return results

 

posted @ 2020-12-24 01:39  风不再来  阅读(107)  评论(0编辑  收藏  举报