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.
#
'''
1. 아이디어 :
2. 시간복잡도 :
3. 자료구조 :
'''
class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
s1="balloon"
chars={}
for c in text:
if c not in chars:
chars[c]=1
else:
chars[c]+=1
if "l" in chars:
chars["l"]=int(chars["l"]/2)
if "o" in chars:
chars["o"]=int(chars["o"]/2)
ans=float("inf")
for s in s1:
if s not in chars:
return 0
else:
ans=min(ans, chars[s])
return ans
'알고리즘 문제 > Leetcode' 카테고리의 다른 글
507. Perfect Number (0) | 2023.03.29 |
---|---|
674. Longest Continuous Increasing Subsequence (0) | 2023.03.29 |
266. Palindrome Permutation (0) | 2023.03.28 |
303. Range Sum Query - Immutable (0) | 2023.03.28 |
203. Remove Linked List Elements (0) | 2023.03.28 |