这是小川的第416次更新,第449篇原创

看题和准备

今天介绍的是LeetCode算法题中Easy级别的第267题(顺位题号是1189)。给定一个字符串文本,使用文本字符来构成单词"balloon"的尽可能多的实例。每个字符最多可以在文本中使用一次。返回可以形成的最大实例数。

例如:

输入:text = "nlaebolko"

输出:1

输入:text = "loonbalxballpoon"

输出:2

输入:text = "leetcode"

输出:0

约束

  • 1 <= text.length <= 10^4
  • 文本字符串仅包含小写英文字母。

第一种解法

题目的意思是要在一个给定的字符串中,找到能够组成字符串"balloon"的最大字符对数,本质上和木桶装水的容量由短板决定类似。

直接遍历text字符串中的字符,对字母ablno的出现次数计数,因为l和o是需要两个,在计数完后,需要对lo的次数除2,然后比较5个字母出现次数的最小值,因为只有出现次数最小的那个字母才能最终决定组成多少个"balloon"

public int maxNumberOfBalloons(String text) {
if (text == "" || text.length() < 7) {
return 0;
}
int A = 0, B = 0, L = 0, O = 0, N = 0;
int len = text.length();
for (int i=0; i<len; i++) {
if (text.charAt(i) == 'a') {
A++;
} else if (text.charAt(i) == 'b') {
B++;
} else if (text.charAt(i) == 'l') {
L++;
} else if (text.charAt(i) == 'n') {
N++;
} else if (text.charAt(i) == 'o') {
O++;
}
}
L /= 2;
O /= 2;
int min = Math.min(A, B);
min = Math.min(min, N);
if (min == 0) {
return 0;
}
if (L !=0 && O != 0) {
min = Math.min(min, L);
min = Math.min(min, O);
return min;
}
return 0;
}

第二种解法

针对第一种解法里,在循环中判断字母的方式,可以换成使用一个长度为26的int数组,根据26个英文字母的顺序,直接从数组中取值即可,最后返回5个数中的最小值。

public int maxNumberOfBalloons2(String text) {
if (text == "" || text.length() < 7) {
return 0;
}
int A = 0, B = 0, L = 0, O = 0, N = 0;
int len = text.length();
int[] arr = new int[26];
for (int i=0; i<len; i++) {
arr[text.charAt(i)-'a']++;
}
A = arr[0];
B = arr[1];
L = arr[11]/2;
N = arr[13];
O = arr[14]/2;
int min = Math.min(A, B);
min = Math.min(min, N);
min = Math.min(min, L);
min = Math.min(min, O);
return min;
}

第三种解法

针对第二种解法,我们可以将5个局部变量也省略掉,毕竟只是比较最小值,直接去数组里取就行。

public int maxNumberOfBalloons3(String text) {
if (text == "" || text.length() < 7) {
return 0;
}
int len = text.length();
int[] arr = new int[26];
for (int i=0; i<len; i++) {
arr[text.charAt(i)-'a']++;
}
int min = Math.min(arr[0], arr[1]); //a b
min = Math.min(min, arr[13]); // n
min = Math.min(min, arr[11]/2); // l
min = Math.min(min, arr[14]/2); // o
return min;
}

小结

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

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

LeetCode.1189-balloon实例数最大值(Maximum Number of Balloons)的更多相关文章

  1. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

  2. 【leetcode】1072. Flip Columns For Maximum Number of Equal Rows

    题目如下: Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and ...

  3. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  4. 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...

  5. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  7. LeetCode 414. Third Maximum Number (第三大的数)

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  8. C#版 - Leetcode 414. Third Maximum Number题解

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

  9. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

随机推荐

  1. Error creating bean with name 'documentationPluginsBootstrapper' defined in URL

    启动报错 Error starting ApplicationContext. To display the auto-configuration report re-run your applica ...

  2. mybatis中foreach使用方法

    作者:学无先后 达者为先 作者:偶尔记一下 foreach一共有三种类型,分别为List,[](array),Map三种. 下面表格是我总结的各个属性的用途和注意点. foreach属性 属性 描述 ...

  3. API接口防止参数篡改和重放攻击

    {近期领导要求我对公司业务的支付类的ocr接口做研究,是否存在支付接口重放攻击,so.....} API重放攻击(Replay Attacks)又称重播攻击.回放攻击.他的原理就是把之前窃听到的数据原 ...

  4. spring @Primary-在spring中的使用(转)

    在spring 中使用注解,常使用@Autowired, 默认是根据类型Type来自动注入的.但有些特殊情况,对同一个接口,可能会有几种不同的实现类,而默认只会采取其中一种的情况下 @Primary ...

  5. vue cli 3.x 配置使用 sourceMap

    项目使用vue cli 3.x搭建,没有了配置文件,如何更方便的查找到对应的scss文件,配置项目支持sourceMap方式? 分二步走: 1.项目根目录(不是src目录,不要搞错了)添加vue.co ...

  6. ila核数据输出

    在Tcl Console中输入以下命令(其中dataxxxx表示文件名,hw_ila_2则为ila窗口名): write_hw_ila_data -csv_file -force dataxxxx [ ...

  7. Luogu P4109 [HEOI2015]定价 贪心

    思路:找规律?$or$贪心. 提交:1次 题解: 发现:若可以构成$X0000$,答案绝对不会再在数字最后把$0$改成其他数: 若可以构成$XX50...0$更优. 所以左端点增加的步长是增加的($i ...

  8. CF1230 E. Kamil and Making a Stream gcd+暴力

    比赛的时候TLE,第二天发现合并方向合并错了~ 改了一下顺序就切了~ 又掉分了,好难过QAQ...... Code: #include <bits/stdc++.h> #define N ...

  9. 【csp模拟赛1】铁路网络 (network.cpp)

    [题目描述] 在暑假来临之际,小 Z 的地理老师布置了一个暑假作业,让同学们暑假期间 了解一下 C 国的铁路发展史.小 Z 在多番查证资料后发现,C 国在铁路发展初期, 铁路网络有着一个严密规整的结构 ...

  10. topcoder13444

    CountTables TopCoder - 13444 sol:题意和题解都丢在上面了,自己XJByy了一下 先保证行不同,然后对列容斥,dp[i]表示i列的答案 行不同时i列的答案显然是C(c^i ...