[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" ...
随机推荐
- Java实用小工具
工具一:对Java中的List<Map<String,Object>>格式数据实现递归 /** * 递归List<Map<String,Object>> ...
- html_js
<!-- js的特点:别名脚本 -由浏览器内置的JavaScript引擎执行代码. -解析执行:事先不编译,逐行执行 -面向对象:内置大量的现成对象 适宜: -客户端的数据计算:不需要保存和提交 ...
- 不使用.h .lib文件使用DLL内的函数
#include <windows.h> typedef int (*Func)(const char *fmt, ...); //这里声明一个函数指针,typedef 关键字是必须的,好 ...
- sed使用案例
简介: sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用 ...
- 反编译查看printf()的方法
源代码: package test2; public class ExplorationJDKSource { /** * @param args */ public static void main ...
- Linux 文件上传
Linux 文件上传操作 SecureCRT与linux互相上传和下载文件 1. 需要上传或者下载,需要使用rz和sz命令. 2. 如果linux上没有这两个命令工具,则需要先安装.可以使用yum安装 ...
- 拉格朗日乘子(Lagrange multify)和KKT条件
拉格朗日乘子(Lagrange multify)和KKT条件 无约束问题 无约束问题定义如下: f(x)称为目标函数, 其中x是一个向量,它的维度是任意的. 通过求导, 令导数等于零即可: 如下图所示 ...
- java中abstract怎么使用
abstract(抽象)修饰符,可以修饰类和方法 1,abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型,抽象类就像当于一类的半 ...
- C#压缩解压zip 文件
/// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...
- rds分区实践
1.查看分区情况 SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'tab ...