Meta Data#
Difficulty: easy First Attempt: 2022-10-18
- Submissions: Runtime Error → Wrong Answer ×4 → Accepted → Runtime Error ×3 → Wrong Answer → Accepted ×2
Approach#
balloon needs one each of b, a, n and two each of l, o. Count text (Hash Table Template 1); each letter supports cnt[c] // need[c] balloons, and the answer is the minimum over the five.
My Attempts#
This took 12 submissions:
- Attempt 1 assigned the letter
sitself toresinstead of its countC[s](TypeError). - The next four filtered with
C[s] < resfirst, comparing the raw count rather than the halved one, so a singlelfailed1 < 1and was skipped:"nolab"should be 0; they returned 1. - Moving to a walrus first caused syntax errors, then
(new := C[s] // 2 < res)boundnewto the comparisonTrue:"ban"returnedTrue. Moving the parenthesis to(new := C[s] // 2) < respassed.
Solution#
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
cnt = Counter(text)
need = Counter("balloon")
return min(cnt[c] // need[c] for c in need)
Complexity#
Time O(n), space O(1).
Mistakes to Avoid#
- Divide by the needed amount first, then take the minimum; do not filter with raw counts.
- Parenthesize the walrus:
(new := x // 2) < res, otherwisenewgets the comparison result.
Interview Explanation#
I count text, divide each letter’s count by how many balloon needs (two for l and o), and take the minimum of the five. Time O(n).
