这是小川的第412次更新,第444篇原创

看题和准备

今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170)。在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字符的出现频率。例如,如果s ="dcce",则f(s)= 2,因为最小字符为"c",其频率为2。

现在,给定字符串数组querieswords,返回一个整数数组answer

其中每个answer[i]是使得f(queries[i]) < f(W)的单词数量,其中Wwords中的单词。

例如:

输入:queries = ["cbd"], words = ["zaaaz"]

输出:[1]

说明:在第一个查询中,我们有f("cbd")= 1f("zaaaz")= 3,因此f("cbd")<f("zaaaz")

输入:queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]

输出:[1,2]

说明:在第一个查询中,仅f("bbb")<f("aaaa"),所以answer[0] = 1

在第二个查询中,f("cc")<f("aaa")f("cc")<f("aaaa"),所以answer[1] = 2

注意

  • 1 <= queries.length <= 2000
  • 1 <= words.length <= 2000
  • 1 <= queries[i].lengthwords[i].length <= 10
  • queries[i][j]words[i][j]是英文小写字母。

第一种解法

题目的意思是要求在words中,找出最小字符出现次数小于queries中字符串最小字符出现次数的单词个数,最后以queries的长度作为int数组返回。

因此,我们直接翻译题目即可,使用两层循环,外层循环遍历queries中的字符串,找到queries[i]中最小字符的出现次数,接着遍历words中的单词,比较两个最小字符出现次数,如果queries中的比较小,就计数,内层循环结束后,将计数结果添加到answer数组中,最后返回。

public int[] numSmallerByFrequency(String[] queries, String[] words) {
int len = queries.length, len2 = words.length;
int[] result = new int[len];
for (int i=0; i<len; i++) {
int query = minFrequency(queries[i]);
int count = 0;
for (int j=0; j<len2; j++) {
int word = minFrequency(words[j]);
if (query < word) {
count++;
}
}
result[i] = count;
}
return result;
} /**
* 找到字符串中的最小字符的出现次数
* @param s
* @return
*/
public int minFrequency(String s) {
int[] arr = new int[26];
int count = 0;
for (int i=0; i<s.length(); i++) {
arr[s.charAt(i)-'a']++;
}
for (int j=0; j<26; j++) {
if (arr[j] != 0) {
count = arr[j];
break;
}
}
return count;
}

第二种解法

针对第一种解法中,在内层循环多次计算words中单词的最小字符出现次数,我们可以抽到循环外面处理。先将每个单词的最小字符出现次数都算出来,存入一个长度和words相同的int数组中,在内层循环中,就可以直接遍历这个int数组了,而不用每个全部重新算一遍。

public int[] numSmallerByFrequency2(String[] queries, String[] words) {
int len = queries.length, len2 = words.length;
int[] wordFreq = new int[len2];
for (int j=0; j<len2; j++) {
wordFreq[j] = minFrequency(words[j]);
}
int[] result = new int[len];
for (int i=0; i<len; i++) {
int query = minFrequency(queries[i]);
int count = 0;
for (int j=0; j<len2; j++) {
if (query < wordFreq[j]) {
count++;
}
}
result[i] = count;
}
return result;
} /**
* 找到字符串中的最小字符的出现次数
* @param s
* @return
*/
public int minFrequency(String s) {
int[] arr = new int[26];
int count = 0;
for (int i=0; i<s.length(); i++) {
arr[s.charAt(i)-'a']++;
}
for (int j=0; j<26; j++) {
if (arr[j] != 0) {
count = arr[j];
break;
}
}
return count;
}

第三种解法

对于前面两种解法,我们还能再简化下吗?比如,将两层循环变成一层循环?要想变一层循环,那么在计算queries中的字符串时,就需要一次拿到结果,不使用循环,也就是说像在数组中取值一样。

我们先来观察下第二个例子。在处理完words中的单词时,会得到一个数组wordFreq,在此基础上再做下变化,做计数处理,将最小字符出现次数作为新数组的索引,再来累计次数,就会得到下面四个:

count[4] = 1; //"aaaa"代表的单词
count[3] = 1; //"aaa"代表的单词
count[2] = 1; //"aa"代表的单词
count[1] = 1; //"a"代表的单词

再来观察下queries数组,字符串"bbb""cc",去和words中的单词比较时,会有以下规律:

大于"bbb"的有1位,记为arr[3] = 1
大于"cc"的有2位,记为arr[2] = 2

如果接着往下写:

大于1的有3位,arr[1] = 3
大于0的有4位,arr[0] = 4

我们发现,以queries中的字符串最小字符出现次数为索引的arr数组,其实就是上面count数组的倒序元素累加之和:

arr[3] = count[4] = 1;
arr[2] = arr[3] + count[3] = 1+1 = 2

即:

arr[i-1] = arr[i]+count[i];

分析出来这其中的原理后,剩下就是将代码写出来了,此解法比前面两种解法速度上快很多。

public int[] numSmallerByFrequency3(String[] queries, String[] words) {
int len = queries.length;
int[] wordFreq = new int[11];
for (String word : words) {
wordFreq[minFrequency(word)]++;
}
int[] sum = new int[11];
for (int i=sum.length-1; i>0; i--) {
sum[i-1] = sum[i]+wordFreq[i];
}
int[] result = new int[len];
for (int i=0; i<len; i++) {
result[i] = sum[minFrequency(queries[i])];
}
return result;
} /**
* 找到字符串中的最小字符的出现次数
* @param s
* @return
*/
public int minFrequency(String s) {
int[] arr = new int[26];
int count = 0;
for (int i=0; i<s.length(); i++) {
arr[s.charAt(i)-'a']++;
}
for (int j=0; j<26; j++) {
if (arr[j] != 0) {
count = arr[j];
break;
}
}
return count;
}

小结

算法专题目前已更新LeetCode算法题文章269+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)的更多相关文章

  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 解题报告(C++)

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

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

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

  5. leetCode 题解之字符串中第一个不重复出现的字符

    1.题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doe ...

  6. C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  7. leetcode python反转字符串中的单词

    # Leetcode 557 反转字符串中的单词III### 题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. **示例1:** 输入: "L ...

  8. Python2.7.3移除字符串中重复字符(一)

    移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...

  9. Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例

    1. 首先我们看看统计字符串中每个字符出现的次数的案例图解: 2. 代码实现: (1)需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5) ...

随机推荐

  1. P5357 【模板】AC自动机(二次加强版)

    思路 这题可以同时作为AC自动机和SAM的模板啊喂 AC自动机 对T建出AC自动机,把S在上面匹配,然后记录每个点被经过的次数,最后统计一次即可(暴力跳fail的复杂度是不对的) SAM 对S建出SA ...

  2. UOJ117 欧拉回路[欧拉回路]

    找欧拉回路的模板题. 知识点详见图连通性学习笔记. 注意一些写法上的问题. line37&line61:因为引用,所以j和head值是同步更新的,类似于网络流的当前弧优化,除了优化枚举外,这样 ...

  3. node.js----一个httpserver提交和解析get参数的例子

    前端代码 <!doctype html> <html lang="en"> <head> <meta charset="utf- ...

  4. 【JAVA-算法】 截取2个字符中间的字符串

    Java Code /** 截取2个字符中间的字符串 */ private void GetMiddleString() { String str = "BB022220011BB007EB ...

  5. encodeURI()、encodeURIComponent()、escape()

    URI的通用格式如下: /*** 协议://用户名:密码@子域名.域名.顶级域名:端口号/目录/文件名.文件后缀?参数1=值1&参数2=值2+值3#标志 **/ /*** http://use ...

  6. CPU结构及段地址偏移地址的概念

    原文地址:http://blog.csdn.net/yihuiworld/article/details/7533335#comments 程序如何执行: CPU先找到程序在内存中的入口地址 -- 地 ...

  7. 洛谷P2135 方块消除

    洛谷题目链接 动态规划(真毒瘤!) 变量声明: $val[i]$:表示第$i$块颜色 $num[i]$:表示第$i$块颜色数量 $sum[i]$:表示$num$的前缀和 我们设计状态$f[l][r][ ...

  8. [Luogu] 文艺平衡树(Splay)

    题面:https://www.luogu.org/problemnew/show/P3391 题解:https://www.zybuluo.com/wsndy-xx/note/1138892

  9. linux如何查看ip地址

    使用命令: ifconfig -a 例如:

  10. OnUpdateError

    DataSetProvider1.OnUpdateError void __fastcall TFrmItem::Query1UpdateError(TDataSet *ASender, EFDExc ...