-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-Solution-N1189.py
More file actions
39 lines (30 loc) · 1.44 KB
/
LeetCode-Solution-N1189.py
File metadata and controls
39 lines (30 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# LeetCode Problem N1189: Maximum Number of Balloons
# Difficulty: Easy
from collections import Counter
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
# Step 1: Count the occurrences of each character in the input text
hmap = Counter(text)
# Step 2: Initialize a list to store the minimum occurrences of each character needed for "balloon"
occurences = []
# Step 3: Check the occurrences of each character in "balloon"
for letter in "balloon":
# If the character is not in the hashmap or not enough occurrences, return 0
if letter not in hmap:
return 0
break
# If the character is 'l' or 'o', we need to divide the count by 2
else:
occurences.append(int(hmap[letter]/"balloon".count(letter)))
# Step 4: Return the minimum occurrences found
if not occurences:
return 0
else:
return min(occurences)
text = "nlaebolko"
text1 = "loonbalxballpoon"
solution = Solution()
print(solution.maxNumberOfBalloons(text)) # Output: 1
print(solution.maxNumberOfBalloons(text1)) # Output: 2
# Time Complexity: O(n), where n is the length of the input string text.
# Space Complexity: O(1), since the size of the hashmap is constant (limited to the characters in "balloon").