[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" ...
随机推荐
- CF1217B Zmei Gorynich
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like rept ...
- 《Thinking in Java》中讲到了final,发现自己有部分地方迷糊
1.1当给全局的静态字段加上final时,系统是不会给其赋默认值的,若不手动初始化,会编译时错误——Variable 'xxx' might not have been initialized. 1. ...
- python的debug神器PySnooper
同事给我推荐了这个调试神器,一直没工夫看,今天看了下. 原文链接: 史上最方便的Python Debug工具|腾讯技术说 体验了下,感觉最好的用法:1.优先逐行调试:2.一些复杂状态处理或者偶现的bu ...
- Python Learning Day1
字符串的操作 # 字符串的操作 str1 = 'my name is xxx, my age is 18.' # 优先掌握的操作: # 1.按索引取值(正向取+反向取) :只能取 print(str1 ...
- html标签title属性效果优化
html标签title属性效果不友好,最致命的是响应慢,体验不好,JQuery-UI提供了很好的支持.在jquery.tip的基础上又做了修改,这样子定制效果更强. 代码如下: <!DOCTYP ...
- textField 基本属性
_textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textField.text = str; [_t ...
- (转)绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程
以系统的一个“添加商品”的功能为例加以说明,系统页面为add.jsp,如图一所示: 图一 添加商品界面 系统的代码目录结构及add.jsp代码如图二所示: 图二 系统的代码目录结构及add.js ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM - 改变 HTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- hadoop cmd
一.hadoop文件操作 1.Ls hadoop fs -ls / 2.Put hadoop fs -put xx /path 3.Mkdir hadoop fs -mkdir 4.要从HDFS中删除 ...
- “杀死”纸质名片!HiHello能重构商业关系网吗?
在当下的互联网时代,要添加好友去扩大自己的社交圈似乎是再简单不过.随便点击一个微信名片.与其他网友互相关注微博等,好像就又搭建了一个社交节点.暂且不讨论这些好友关系的质量问题,单是这样的方式并不适合于 ...