1189. “气球” 的最大数量
给你一个字符串 text
,你需要使用 text
中的字母来拼凑尽可能多的单词 “balloon”(气球)。
字符串 text
中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 “balloon”。
示例 1:
输入:text = "nlaebolko"
输出:1
示例 2:
输入:text = "loonbalxballpoon"
输出:2
示例 3:
输入:text = "leetcode"
输出:0
提示:
1 <= text.length <= 10^4
text
全部由小写英文字母组成public class Solution1189 {
public int maxNumberOfBalloons(String text) {
int num = 0;
String balloon = "balloon";
boolean find = true;
int len1 = 0;
int len2 = 0;
while (find == true) {
len1 = text.length();
for (int i = 0; i < balloon.length(); i++) {
text = text.replaceFirst(balloon.substring(i, i + 1), "");
}
len2 = text.length();
if (len1 - len2 == balloon.length()) {
num++;
find = true;
} else {
find = false;
}
}
return num;
}
public static void main(String[] args) {
Solution1189 s = new Solution1189();
String text = "nlaebolko";
System.out.println(s.maxNumberOfBalloons(text));
}
}
还没有评论,来说两句吧...