作者: 负雪明烛
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. 【豆科基因组】普通豆/菜豆/四季豆Common bean (Phaseolus vulgaris L.) 683个自然群体重测序2020NG

    目录 一.来源 二.结果 683份材料重测序 地方种landraces和育种品系breeding lines的多样性 表型和基因-环境互作(G by E) 菜豆产量潜力相关的MTAs(显著关联位点) ...

  2. PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组

    PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 <?php /** * 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 * @param [type] $st ...

  3. JavaScript获取html表单值验证后跳转网页中的关键点

    关键代码: 1.表单部分 <form action="Depart.jsp" name="myform" method="post" ...

  4. Hadoop入门 集群崩溃的处理方法

    目录 集群崩溃的处理方法 搞崩集群 错误示范 正确处理方法 1 回到hadoop的家目录 2 杀死进程 3 删除每个集群的data和logs 4 格式化 5 启动集群 总结 原因分析 集群崩溃的处理方 ...

  5. 【STM8】SPI通讯

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

  6. Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap (1)

    从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念.传统的socket是基于s ...

  7. Mybatis 批量插入

    一.首先对于批量数据的插入有两种解决方案(下面内容只讨论和Mysql交互的情况) 1)for循环调用Dao中的单条插入方法 2)传一个List<Object>参数,使用Mybatis的批量 ...

  8. ReactiveCocoa操作方法-重复

    retry重试      只要失败,就会重新执行创建信号中的block,直到成功. __block int i = 0; [[[RACSignal createSignal:^RACDisposabl ...

  9. 如何使用redis作为缓存,增强用户访问数据的用户体验

    /**完成步骤 1.创建关系型数据库mysql的Provice库,同时启动nosql系列的redis数据库 2.创建项目,导入相关的jar包 3.创建jedis/utils/domain/dao/se ...

  10. MyEclipse配置Spring框架(基础篇)

    一.新建项目,添加spring的相关jar包等 二.创建相关类以及属性和方法 Student.java package com.yh; public class Student implements ...