【LeetCode】Compare Strings by Frequency of the Smallest Character

序号:1170

难度:easy

Let's define a function f(s) over a non-empty string s, which calculates the frequency of the smallest character in s. For example, if s = "dcce" then f(s) = 2 because the smallest character is "c" and its frequency is 2.

Now, given string arrays queries and words, return an integer array answer, where each answer[i] is the number of words such that f(queries[i]) < f(W), where W is a word in words.

 

Example 1:

Input: queries = ["cbd"], words = ["zaaaz"]
Output: [1]
Explanation: On the first query we have f("cbd") = 1, f("zaaaz") = 3 so f("cbd") < f("zaaaz").
Example 2:

Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
Output: [1,2]
Explanation: On the first query only f("bbb") < f("aaaa"). On the second query both f("aaa") and f("aaaa") are both > f("cc").
 

Constraints:

1 <= queries.length <= 2000
1 <= words.length <= 2000
1 <= queries[i].length, words[i].length <= 10
queries[i][j], words[i][j] are English lowercase letters.

方法一:

  首先计算出words中每个字符串的f(W),之后逐个计算quries中字符串的f(c),在与f(W)逐个比较。

   w -> words.length,  q -> queries.length

  时间复杂度:O(w*q)

  空间复杂度:O(w+q)

class Solution {
  public int[] numSmallerByFrequency(String[] queries, String[] words) {
            int[] ans = new int[queries.length];
            int[] wordsCount = new int[words.length];
            for(int i = 0;i < words.length;i++)
                wordsCount[i] = countNum(words[i]);
            for(int i = 0;i < queries.length;i++) {
                for(int j = 0;j < wordsCount.length;j++) {
                    if(countNum(queries[i]) < wordsCount[j])
                        ans[i] += 1;
                }
            }
            return ans;
        }
        
        public int countNum(String s) {
            int[] arr = new int[26];
            int min = 1;
            for(int i = 0;i < s.length();i++) {
                arr[s.charAt(i) - 'a'] += 1;
            }
            for(int i = 0 ;i < 26;i++)
                if(arr[i] != 0)
                    return arr[i];
            return 1;
            
        }
}

方法二:

  用一个数组下标表示“出现次数”,其元素表示大于此下标的字符串出现次数。

  Time: O(w*10) + O(q) = O(w+q)

  Space: O(11 + q) = O(q)

public int[] numSmallerByFrequency(String[] queries, String[] words) {
        
        int[] result = new int[queries.length];
        int[] wordsFrequency = new int[11];

        for(int i = 0; i < words.length; i++){
            int runningFreq = calculateFrequency(words[i]);
            wordsFrequency[runningFreq - 1]++;
        }

        for(int i = 9; i >= 0; i--){
            wordsFrequency[i] = wordsFrequency[i] + wordsFrequency[i + 1];
        }

        for(int i = 0; i < queries.length; i++){
            int freq = calculateFrequency(queries[i]);
            result[i] = wordsFrequency[freq];
        }

        return result;
    }

    private static int calculateFrequency(String string) {
        int count = 1;
        char smallestChar = string.charAt(0);
        for(int i = 1; i < string.length(); i++){
            if(string.charAt(i) == smallestChar) count++;
            else if(string.charAt(i) < smallestChar){
                smallestChar = string.charAt(i);
                count = 1;
            }
        }

        return count;
    }

 

  

 

posted @ 2020-02-22 16:12  LinM狂想曲  阅读(211)  评论(0编辑  收藏  举报