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").
class Solution {
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int qNum = queries.length;
int wNum = words.length;
int[] qArr = new int[qNum];
int[] wArr = new int[wNum];
for (int i = 0; i < qArr.length; i++) {
qArr[i] = getSmallFreq(queries[i]);
}
for (int i = 0; i < wArr.length; i++) {
wArr[i] = getSmallFreq(words[i]);
}
int[] res = new int[qNum];
for (int i = 0; i < qNum; i++) {
int tmp = 0;
for (int wFreq: wArr) {
if (qArr[i] < wFreq) {
tmp += 1;
}
}
res[i] = tmp;
}
return res;
} private int getSmallFreq(String s) {
int[] freqArr = new int[26];
for (char c: s.toCharArray()) {
freqArr[c - 'a'] += 1;
}
for (int i = 0; i < freqArr.length; i++) {
if (freqArr[i] > 0) {
return freqArr[i];
}
}
return 0;
}
}

[LC] 1170. Compare Strings by Frequency of the Smallest Character的更多相关文章

  1. 【Leetcode_easy】1170. Compare Strings by Frequency of the Smallest Character

    problem 1170. Compare Strings by Frequency of the Smallest Character 参考 1. Leetcode_easy_1170. Compa ...

  2. 【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 ...

  3. 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ...

  4. LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)

    这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...

  5. LintCode 58: Compare Strings

    LintCode 58: Compare Strings 题目描述 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母. 样例 给出A = "ABCD&q ...

  6. Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  7. [LC] 165. Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 &l ...

  8. [LC] 451. Sort Characters By Frequency

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  9. lintcode:Compare Strings 比较字符串

    题目: 比较字符串 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 样例 给出 A = "ABCD" B = "ACD" ...

随机推荐

  1. SAP_MM常用代码

    1.采购申请创建/修改/查看:ME51N/ME52N/ME53N 2.采购申请审批:ME54N 3.采购订单创建/修改/查看:ME21N/ME22N/ME23N 4.单个采购订单审批:ME29N 5. ...

  2. [GWCTF 2019]枯燥的抽奖

    0x00 知识点 种子爆破 工具 http://www.openwall.com/php_mt_seed 0x01 解题 查看源码进入check.php zOHF0Cxp49 <?php #这不 ...

  3. POJ 1013:Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42028   Accepted: 13 ...

  4. LVS三种模式区别

    参考文档 http://www.magedu.com/65436.html 名词:CIP 客户端IP地址   VIP:即DS服务器上的代理IP地址,也是客户端访问的执行IP地址 1.NAT模式 ①.客 ...

  5. SeetaFaceQt:写一个简单的界面

    关于这个界面,我用到了几个控件,这些控件通过Qt是非常容易构建的,窗口的话用的是QWidget,之前说了,QWidget是Qt里面几乎大部分控件的父类,QWidget的布局我使用了简单的水平布局(QH ...

  6. Maven:A cycle was detected in the build path of project 'xxx'. The cycle consists of projects {xx}

    以下这个错误是在Eclipse中导入多个相互依赖的工程时出现的“循环依赖问题”:A cycle was detected in the build path of project 'xxx'. The ...

  7. HashMap实现原理(jdk1.7),源码分析

    HashMap实现原理(jdk1.7),源码分析 ​ HashMap是一个用来存储Key-Value键值对的集合,每一个键值对都是一个Entry对象,这些Entry被以某种方式分散在一个数组中,这个数 ...

  8. 干货 | 把Flutter扩展到微信小程序端的探索

    Google Flutter是一个非常优秀的跨端框架,不仅可以运行在Android. iOS平台,而且可以支持Web和桌面应用.在国内小程序是非常重要的技术平台,我们也一直思考能否把Flutter扩展 ...

  9. 吴裕雄--天生自然MySQL学习笔记:MySQL 连接的使用

    使用 MySQL 的 JOIN 在两个或多个表中查询数据. 可以在 SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询. JOIN 按照功能大致分为如 ...

  10. 吴裕雄--天生自然 PHP开发学习:超级全局变量

    <!DOCTYPE html> <html> <body> <?php $x = 75; $y = 25; function addition() { $GL ...