← Static tasks

1189. Maximum Number of Balloons

leetcode easy

#csharp#easy#leetcode#math#string

Task

Дана строка text. Вы хотите использовать символы строки text, чтобы сформировать как можно больше экземпляров слова "balloon".

Каждый символ строки text можно использовать не более одного раза. Верните максимальное количество экземпляров, которые можно сформировать.

Пример:

Input: text = "nlaebolko"

Output: 1

C# solution

matched/original
public class Solution {
    public int MaxNumberOfBalloons(string text) {
        int bCount = 0, aCount = 0, lCount = 0, oCount = 0, nCount = 0;
        foreach (char c in text) {
            if (c == 'b') {
                bCount++;
            } else if (c == 'a') {
                aCount++;
            } else if (c == 'l') {
                lCount++;
            } else if (c == 'o') {
                oCount++;
            } else if (c == 'n') {
                nCount++;
            }
        }
        lCount /= 2;
        oCount /= 2;
        return Math.Min(bCount, Math.Min(aCount, Math.Min(lCount, Math.Min(oCount, nCount))));
    }
}

C++ solution

auto-draft, review before submit
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int MaxNumberOfBalloons(string text) {
        int bCount = 0, aCount = 0, lCount = 0, oCount = 0, nCount = 0;
        foreach (char c in text) {
            if (c == 'b') {
                bCount++;
            } else if (c == 'a') {
                aCount++;
            } else if (c == 'l') {
                lCount++;
            } else if (c == 'o') {
                oCount++;
            } else if (c == 'n') {
                nCount++;
            }
        }
        lCount /= 2;
        oCount /= 2;
        return min(bCount, min(aCount, min(lCount, min(oCount, nCount))));
    }
}

Java solution

matched/original
class Solution {
    public int maxNumberOfBalloons(String text) {
        int bCount = 0, aCount = 0, lCount = 0, oCount = 0, nCount = 0;

        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == 'b') {
                bCount++;
            } else if (text.charAt(i) == 'a') {
                aCount++;
            } else if (text.charAt(i) == 'l') {
                lCount++;
            } else if (text.charAt(i) == 'o') {
                oCount++;
            } else if (text.charAt(i) == 'n') {
                nCount++;
            }
        }

        lCount = lCount / 2;
        oCount = oCount / 2;

        return Math.min(bCount, Math.min(aCount, Math.min(lCount, Math.min(oCount, nCount))));
    }
}

Python solution

matched/original
class Solution:
    def maxNumberOfBalloons(self, text: str) -> int:
        bCount = 0
        aCount = 0
        lCount = 0
        oCount = 0
        nCount = 0

        for char in text:
            if char == 'b':
                bCount += 1
            elif char == 'a':
                aCount += 1
            elif char == 'l':
                lCount += 1
            elif char == 'o':
                oCount += 1
            elif char == 'n':
                nCount += 1

        lCount = lCount // 2
        oCount = oCount // 2

        return min(bCount, aCount, lCount, oCount, nCount)

Go solution

matched/original
func maxNumberOfBalloons(text string) int {
    bCount, aCount, lCount, oCount, nCount := 0, 0, 0, 0, 0

    for _, c := range text {
        switch c {
        case 'b':
            bCount++
        case 'a':
            aCount++
        case 'l':
            lCount++
        case 'o':
            oCount++
        case 'n':
            nCount++
        }
    }

    lCount /= 2
    oCount /= 2

    return min(bCount, aCount, lCount, oCount, nCount)
}

func min(a, b, c, d, e int) int {
    return min2(min2(a, b), min2(min2(c, d), e))
}

func min2(x, y int) int {
    if x < y {
        return x
    }
    return y
}

Explanation

Algorithm

Подсчитайте количество появлений каждого символа 'b', 'a', 'l', 'o', 'n' в строке text.

Вычислите потенциал для каждого символа: для 'b' и 'a' потенциал равен количеству их появлений, для 'l' и 'o' потенциал равен количеству их появлений, деленному на 2, а для 'n' потенциал равен количеству его появлений.

Найдите символ с наименьшим потенциалом среди 'b', 'a', 'l', 'o', 'n', который ограничивает количество возможных слов "balloon", и верните это значение.

😎