Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Maximum Number of Balloons

Posted on August 5, 2019July 26, 2020 by braindenny

Maximum Number of Balloons



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #hashmap

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 <= 10^4
  • text consists of lower case English letters only.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/maximum-number-of-balloons
// Basic Ideas: hashmap
// Complexity: Time O(n), Space O(1)
func min(x, y int) int {
    if x < y {
        return x
    } else {
        return y
    }
}
func maxNumberOfBalloons(text string) int {
    l := make([]int, 26)
    res := 1<<31-1
    for i, _ := range text {
        l[text[i]-'a']++
    }
    res = min(res, l['b'-'a'])
    res = min(res, l['a'-'a'])
    res = min(res, l['l'-'a']/2)
    res = min(res, l['o'-'a']/2)
    res = min(res, l['n'-'a'])
    return res
}
linkedin
github
slack

Post Views: 0
Posted in BasicTagged hashmap

Post navigation

LeetCode: Shortest Distance to Target Color
LeetCode: Check If Word Is Valid After Substitutions

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.