作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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. 1 <= queries.length <= 2000
  2. 1 <= words.length <= 2000
  3. 1 <= queries[i].length, words[i].length <= 10
  4. queries[i][j], words[i][j] are English lowercase letters.

题目大意

定义f(s)为一个字符串中最小的字符(按字母序abcd…xyz)出现的次数。对于每个query的f(query),求出words中f(word) > f(query)有多少个。

解题方法

双重循环

第一步,肯定要把每个query和word的f(s)求出来,求每个字符的次数,然后找出最小的字符出现的次数。
第二步,找出words中f(word) > f(query)有多少个时,对于2000*2000的量级,可以暴力两重循环,即对每个query都去遍历一次words的f(word)结果。

时间复杂度O(N^2).

这个题可以优化的地方在,求一个容器中有多少元素大于指定值,可以采用先排序再upper_bound()的方式降低时间度;或者利用题目给的限制:字符串的长度最多是10,因此f(s)一定小于等于10,这样可以用字典保存words中每个f(s)的次数,查找的时候直接在遍历字典中累计次数即可。

C++代码如下:

class Solution {
public:
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
vector<int> qs, ws;
for (string& q : queries) {
qs.push_back(getFrequency(q));
}
for (string& w : words) {
ws.push_back(getFrequency(w));
}
vector<int> res;
for (int q : qs) {
int count = 0;
for (int w : ws) {
if (w > q)
count++;
}
res.push_back(count);
}
return res;
}
int getFrequency(string& word) {
vector<int> counts(26, 0);
for (char w : word) {
counts[w - 'a']++;
}
for (int i = 0; i < 26; ++i) {
if (counts[i] != 0)
return counts[i];
}
return 0;
}
};

日期

2019 年 8 月 31 日 —— 赶在月底做个题

【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)的更多相关文章

  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. [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 smalle ...

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

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

  5. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  7. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  8. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  9. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. Mysql 预处理 PREPARE以及预处理的好处

    Mysql 预处理 PREPARE以及预处理的好处 Mysql手册 预处理记载: 预制语句的SQL语法在以下情况下使用:   · 在编代码前,您想要测试预制语句在您的应用程序中运行得如何.或者也许一个 ...

  2. C#筛选项联动,联动筛选

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillDep(); } // FillDG(); ...

  3. SparkStreaming消费Kafka,手动维护Offset到Mysql

    目录 说明 整体逻辑 offset建表语句 代码实现 说明 当前处理只实现手动维护offset到mysql,只能保证数据不丢失,可能会重复 要想实现精准一次性,还需要将数据提交和offset提交维护在 ...

  4. 【STM8】SPI通讯

    这篇内容有点长,如果有人想透过我的博客学习STM8的SPI,那是我的荣幸 首先我要先说大纲,这样大家心里比较有底,可以把精力都用在SPI理解上 [SPI初步介绍]:介绍SPI如何接线.名称解释.通讯注 ...

  5. C++一元多项式求导

    这个题难度不大但是坑有点多,要考虑的点有几个: 1.测试用例为x 0 这个直接输出 0 0即可. 2.注意空格的输出 3.测试点3我好几次都没过,最后参考了别的答案加以修改才通过. 测试点3没过的代码 ...

  6. JavaIO——转换流、字符编码

    1.转换流 转换流是将字节流变成字符流的流. OutputStreamWriter:将字节输出流转换成字符输出流. public class OutputStreamWriter extends Wr ...

  7. android 跳到应用市场给软件评分

    1 String packetName = this.getPackageName(); 2 Uri uri = Uri.parse("market://details?id=" ...

  8. 【面试】【Linux】【Web】基础概念

    1. HTTP https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol 2. TCP handshake https://en.wikipe ...

  9. 【JAVA】【基础知识】Java程序执行过程

    1. Java程序制作过程 使用文本编辑器进行编辑 2. 编译源文件,生成class文件(字节码文件) javac源文件路径. 3.运行程序class文件.

  10. 【Linux】【Services】任务计划、周期性任务执行

    Linux任务计划.周期性任务执行       未来的某时间点执行一次某任务:at, batch     周期性运行某任务:crontab         执行结果:会通过邮件发送给用户        ...