zoukankan      html  css  js  c++  java
  • [LeetCode] 1170. Compare Strings by Frequency of the Smallest Character

    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.

    比较字符串最小字母出现频次。

    我们来定义一个函数 f(s),其中传入参数 s 是一个非空字符串;该函数的功能是统计 s  中(按字典序比较)最小字母的出现频次。

    例如,若 s = "dcce",那么 f(s) = 2,因为最小的字母是 "c",它出现了 2 次。

    现在,给你两个字符串数组待查表 queries 和词汇表 words,请你返回一个整数数组 answer 作为答案,其中每个 answer[i] 是满足 f(queries[i]) < f(W) 的词的数目,W 是词汇表 words 中的词。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/compare-strings-by-frequency-of-the-smallest-character
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题暴力解不难想,但是暴力解是O(n^2)级别的,很显然会超时。我这里提供一个二分法的思路。这道题其实不是很难但是题意写的不是很清楚,f函数 其实需要输出的是字典序最小的出现次数不为0的字母的出现次数。

    首先根据题意我们去实现 f函数 ,这个不是很难。我们创建两个临时数组 int[] q 和 int[] w ,分别把queries和words里面的每个单词跑过F函数的结果存储起来,同时我们把 int[] w 排序,这样找最后结果的时候可以用到二分法。

    时间O(nlogn)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int[] numSmallerByFrequency(String[] queries, String[] words) {
     3         int[] q = new int[queries.length];
     4         int[] w = new int[words.length];
     5         for (int i = 0; i < q.length; i++) {
     6             q[i] = helper(queries[i]);
     7         }
     8         for (int j = 0; j < w.length; j++) {
     9             w[j] = helper(words[j]);
    10         }
    11         Arrays.sort(w);
    12         int[] res = new int[q.length];
    13         for (int i = 0; i < q.length; i++) {
    14             int left = 0;
    15             int right = w.length - 1;
    16             while (left <= right) {
    17                 int mid = left + (right - left) / 2;
    18                 if (q[i] >= w[mid]) {
    19                     left = mid + 1;
    20                 } else {
    21                     right = mid - 1;
    22                 }
    23             }
    24             res[i] = w.length - left;
    25         }
    26         return res;
    27     }
    28 
    29     private int helper(String s) {
    30         int[] count = new int[26];
    31         for (int i = 0; i < s.length(); i++) {
    32             count[s.charAt(i) - 'a']++;
    33         }
    34 
    35         for (int i = 0; i < 26; i++) {
    36             if (count[i] != 0) {
    37                 return count[i];
    38             }
    39         }
    40         return 0;
    41     }
    42 }

    LeetCode 题目总结

  • 相关阅读:
    Git分支合并:Merge、Rebase的选择
    linux系统下MySQL表名区分大小写问题
    Spring Mvc和Spring Boot读取Profile方式
    Git删除远程分支
    TortoiseGit push免输密码
    git [command line] fatal: Authentication failed for
    Jenkins [Error] at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:192)
    FAIL
    WIN下修改host文件并立即生效
    MYSQL 创建数据库SQL
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14127866.html
Copyright © 2011-2022 走看看