[LC] 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").
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的更多相关文章
- 【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 ...
- 【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 ...
- 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ...
- LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)
这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...
- LintCode 58: Compare Strings
LintCode 58: Compare Strings 题目描述 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母. 样例 给出A = "ABCD&q ...
- Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...
- [LC] 165. Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 &l ...
- [LC] 451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- lintcode:Compare Strings 比较字符串
题目: 比较字符串 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 样例 给出 A = "ABCD" B = "ACD" ...
随机推荐
- AT2000 Leftmost Ball 解题报告
题面 给你n种颜色的球,每个球有k个,把这n*k个球排成一排,把每一种颜色的最左边出现的球涂成白色(初始球不包含白色),求有多少种不同的颜色序列,答案对1e9+7取模 解法 设\(f(i,\;j)\) ...
- nginx反向代理和负载均衡的实现
反向代理和负载均衡的关系可以理解为,一个ip的负载均衡就是反向代理. 反向代理使用的是:proxy_pass指令 负载均衡使用的是:proxy_pass指令+upstream指令 负载均衡的3中方 ...
- Codeforces Round #599 (Div. 2) Tile Painting
题意:就是给你一个n,然后如果 n mod | i - j | == 0 并且 | i - j |>1 的话,那么i 和 j 就是同一种颜色,问你最大有多少种颜色? 思路: 比赛的时候,看到 ...
- tfield的字段名和显示名
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- java8中的map 和reduce
map 1.使用map让集合里面的数字翻倍. List<Integer> numbers = Lists.newArrayList(1,2,3,4,5);List<Integer&g ...
- UVA 11552 序列划分模型 状态设计DP
这个题目刚看到还真不好下手,把一个是 k的倍数的长度的字符串分成len/k块,每块是k个字母,每个块可以重新组合,最后使得整个序列的相同字母尽量在一起,也就是说,最后会把序列从前往后扫,相连的相同字母 ...
- ZOJ-1234 UVA-10271 DP
最近觉得动态规划真的很练脑子,对建模以及思维方法有很大帮助,线段树被卡到有点起不来的感觉 最近仔细思考了一下动态规划的思想,无非是由局部最优解得到全局最优解,由此类推,发现,像最短路和最小生成树其实都 ...
- UML-架构分析-基础
1.何时开始架构分析? 最好在第一次迭代前开始.因为,架构分析的失败会导致高风险.如:必须支持英语.在一秒响应时间内支持500个并发事务. UP是迭代和进化的(不是瀑布式的),所以架构分析和开发工作齐 ...
- 使用IDEA搭建SSM,小白教程
本片博客主要是搭建一个简单的SSM框架,感兴趣的同学可以看一下 搭建ssm框架首先我们需要有一个数据库,本篇文章博主将使用一个MySQL的数据,如果没学过MySQL数据库的,学过其他数据库也是可以的 ...
- Linux 系统查看服务器SN序列号以及服务器型号
1.单独查看服务器的序列号 [root@localhost ~]# dmidecode -t system | grep 'Serial Number' Serial Number: 2102310Y ...