Skip to content

Latest commit

 

History

History
202 lines (168 loc) · 4.56 KB

File metadata and controls

202 lines (168 loc) · 4.56 KB

中文文档

Description

Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.

You can use each character in text at most once. Return the maximum number of instances that can be formed.

 

Example 1:

Input: text = "nlaebolko"
Output: 1

Example 2:

Input: text = "loonbalxballpoon"
Output: 2

Example 3:

Input: text = "leetcode"
Output: 0

 

Constraints:

  • 1 <= text.length <= 104
  • text consists of lower case English letters only.

Solutions

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
    }
}

...