题目如下:

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 <= 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的更多相关文章

  1. 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ...

  2. 【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 ...

  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】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

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

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

  6. 【LeetCode】165 - Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  7. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  8. 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...

  9. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

随机推荐

  1. 终于, Delphi XE2 携带 GDI+ 库了

    终于, Delphi XE2 携带 GDI+ 库了 使用了较早的 http://www.progdigy.com uses Winapi.GDIPAPI, Winapi.GDIPOBJ{, Winap ...

  2. 测开之路一百四十二:ORM框架之SQLAlchemy建库、建表、数据库操作

    flask-SQLAlchemy是在原生SQLAlchemy的基础之上做了一层封装,安装flask-SQLAlchemy会自动安装SQLAlchemy 安装 传统的sql建表建字段 通过flask-S ...

  3. sql进阶练习题

    student SNO    SNAME    SAGE    SSEX01    赵雷    1990-01-01 00:00:00    男02    钱电    1990-12-21 00:00 ...

  4. HTTP学习记录:三、状态码

    学习资源主要为:@小坦克HTTP相关博客 常见的HTTP状态码: 200--请求成功: 302--重定向: 304--Not Modified:表示上次的文档已经被缓存了,还可以继续使用: 400-- ...

  5. Java ——接口

    本节重点思维导图 定义: public interface Traffic { public static final int sits = 4; public abstract void run() ...

  6. Ubuntu新建用户以及安装pytorch

    环境:Ubuntu18,Python3.6 首先登录服务器 ssh username@xx.xx.xx.xxx #登录一个已有的username 新建用户 sudo adduser username ...

  7. git.ZC一套命令_稀疏签出(sparse-checkout)

    1. git init git remote add origin https://gitee.com/?????/movieHome.git git config core.sparsechecko ...

  8. 大数据平台CentOS7+CDH5.12.1集群搭建

    1.CM(Cloudera Manager)介绍 1.1 简介 Cloudera Manager是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具,使得安装集群从几天的时间缩短在几个小 ...

  9. c++primer chapter three

    3.1命名空间的using声明 using声明具有如下的形式:using namespace :: name; #include <iostream> using std :: cout; ...

  10. 高性能迷你React框架anujs1.1.3发布

    anujs现在只差一个组件(mention)就完全支持阿里的antd UI库了.一共跑通346个测试, 应该是全世界最接近官方React的迷你框架了. 以后的工作就是把React16的一些新特性支持了 ...