【leetcode】1170. Compare Strings by Frequency of the Smallest Character
题目如下:
Let's define a function
f(s)
over a non-empty strings
, which calculates the frequency of the smallest character ins
. For example, ifs = "dcce"
thenf(s) = 2
because the smallest character is"c"
and its frequency is 2.Now, given string arrays
queries
andwords
, return an integer arrayanswer
, where eachanswer[i]
is the number of words such thatf(queries[i])
<f(W)
, whereW
is a word inwords
.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 <= queries.length <= 2000
1 <= words.length <= 2000
1 <= queries[i].length, words[i].length <= 10
queries[i][j]
,words[i][j]
are English lowercase letters.
解题思路:本题比较简单,先求出words中每个单词的最小字母的出现频次,并保存到list中。接下来计算queries中每个单词的最小字母的出现频次,并与words中的频次比较。比较的方法可以用二分查找,这样很快就能得到结果。
代码如下:
class Solution(object):
def numSmallerByFrequency(self, queries, words):
"""
:type queries: List[str]
:type words: List[str]
:rtype: List[int]
"""
def calc(word):
min_v = word[0]
dic = {}
for i in word:
dic[i] = dic.setdefault(i,0) + 1
min_v = min(min_v,i)
return dic[min_v]
words_count = []
for word in words:
words_count.append(calc(word)) words_count.sort() res = []
import bisect
for query in queries:
count = calc(query)
inx = bisect.bisect_right(words_count,count)
res.append(len(words_count) - inx)
return res
【leetcode】1170. Compare Strings by Frequency of the Smallest Character的更多相关文章
- 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ...
- 【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 ...
- [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 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)
这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
- 【LeetCode】859. Buddy Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
随机推荐
- php5.4编译安装apache
1.下载源码包 wget 网址/php-5.4.45.tar2.解压源码包 tar -zxvf php-5.4.45.tar3.创建一个安装目录 mkdir /usr/local/php4.进入解压后 ...
- delphi将程序最小化至右下角
程序新手,如果有不恰当的地方,请大家帮忙改正! 1.下载并安装Raize.v5.5控件,delphi版本为:delphi 7.0. 2.添加RzTrayIcon控件.PopupMenu控件至窗体上. ...
- C# 打印倒三角
void test6(int num) { try { #region 方法1 int maxstar = (num - 1) * 2 + 1; string line = ""; ...
- idea 使用过程中的一些设置记录
1.XML sql底色 在mapper文件中,SQL出现黄色的底色. ** settings ** > ** inspections ** 去掉 ** SQL ** 中的 ** No data ...
- vue组件之间通信总结---点赞
总结:父组件-->子组件 ①通过属性 步骤1: <son myName="michael" myPhone='123'></son> <son ...
- 【Qt开发】foreach用法
If you just want to iterate over all the items in a container in order, you can use Qt's foreach key ...
- 从零构建vue项目(一)--搭建node环境,拉取项目模板
本文是基于vuecli2搭建的项目. 1. 下载安装nodejs 地址:https://nodejs.org/en/download/ 选择安装版windows .msi, 不要选择压缩版 下 ...
- 从企业版BOSS直聘,看求职简历技巧
有时候,不是我们不可以,而是我们连面试的机会都没有.希望这篇文章能帮助大家找工作,有一个展示自己的机会. [ ] 最近负责测试的面试工作,在等HR推简历的时候害怕错过优秀的伙伴,找HR拿到了公司在BO ...
- CentOS7 源进源出
echo 200 ct >> /etc/iproute2/rt_tablesecho 201 cu >> /etc/iproute2/rt_tables ip route ad ...
- 主机(windows10)虚拟机(ubuntu18)arm板(linux3.4)相互ping通
实际中在主机上安装虚拟机,并在主机上通过网线连接arm板进行调试. 用网线将主机和arm板直接物理连接,且主机和arm必须处于同一个网段.(我们知道主机中的网卡具有路由器的功能) 其中arm板IP地址 ...