Skip to content

Latest commit

 

History

History
211 lines (170 loc) · 4.8 KB

File metadata and controls

211 lines (170 loc) · 4.8 KB

English Version

题目描述

给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)

字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"

 

示例 1:

输入:text = "nlaebolko"
输出:1

示例 2:

输入:text = "loonbalxballpoon"
输出:2

示例 3:

输入:text = "leetcode"
输出:0

 

提示:

  • 1 <= text.length <= 10^4
  • text 全部由小写英文字母组成

解法

简单计数。

Python3

class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        counter = Counter(text)
        counter['l'] >>= 1
        counter['o'] >>= 1
        return min(counter[c] for c in 'balon')

Java

class Solution {
    public int maxNumberOfBalloons(String text) {
        int[] counter = new int[26];
        for (char c : text.toCharArray()) {
            ++counter[c - 'a'];
        }
        counter['l' - 'a'] >>= 1;
        counter['o' - 'a'] >>= 1;
        int ans = 10000;
        for (char c : "balon".toCharArray()) {
            ans = Math.min(ans, counter[c - 'a']);
        }
        return ans;
    }
}

TypeScript

function maxNumberOfBalloons(text: string): number {
    let targets: Set<string> = new Set('balloon'.split(''));
    let cnt = new Array(126).fill(0);
    for (let char of text) {
        if (targets.has(char)) {
            cnt[char.charCodeAt(0)]++;
        }
    }
    cnt['l'.charCodeAt(0)] >>= 1;
    cnt['o'.charCodeAt(0)] >>= 1;
    let ans = Number.MAX_SAFE_INTEGER;
    for (let char of targets) {
        ans = Math.min(cnt[char.charCodeAt(0)], ans);
    }
    return ans;
}
function maxNumberOfBalloons(text: string): number {
    const map = new Map([
        ['b', 0],
        ['a', 0],
        ['l', 0],
        ['o', 0],
        ['n', 0],
    ]);
    for (const c of text) {
        if (map.has(c)) {
            map.set(c, map.get(c) + 1);
        }
    }
    map.set('l', Math.floor(map.get('l') / 2));
    map.set('o', Math.floor(map.get('o') / 2));
    let res = Infinity;
    for (const value of map.values()) {
        res = Math.min(res, value);
    }
    return res;
}

C++

class Solution {
public:
    int maxNumberOfBalloons(string text) {
        vector<int> counter(26);
        for (char& c : text) ++counter[c - 'a'];
        counter['l' - 'a'] >>= 1;
        counter['o' - 'a'] >>= 1;
        int ans = 10000;
        string t = "balon";
        for (char& c : t) ans = min(ans, counter[c - 'a']);
        return ans;
    }
};

Go

func maxNumberOfBalloons(text string) int {
	counter := make([]int, 26)
	for i := range text {
		counter[text[i]-'a']++
	}
	counter['l'-'a'] >>= 1
	counter['o'-'a'] >>= 1
	ans := 10000
	t := "balon"
	for i := range t {
		ans = min(ans, counter[t[i]-'a'])
	}
	return ans
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Rust

impl Solution {
    pub fn max_number_of_balloons(text: String) -> i32 {
        let mut arr = [0; 5];
        for c in text.chars() {
            match c {
                'b' => arr[0] += 1,
                'a' => arr[1] += 1,
                'l' => arr[2] += 1,
                'o' => arr[3] += 1,
                'n' => arr[4] += 1,
                _ => {}
            }
        }
        arr[2] /= 2;
        arr[3] /= 2;
        let mut res = i32::MAX;
        for num in arr {
            res = res.min(num);
        }
        res
    }
}

...