We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Strings
- The Minion Game
- Discussions
The Minion Game
The Minion Game
Sort by
recency
|
1329 Discussions
|
Please Login in order to post a comment
This is the best ans :
for i in range(n): if string[i] in vowels: kevin += n - i else: stuart += n - i
if stuart > kevin: print("Stuart", stuart) elif kevin > stuart: print("Kevin", kevin) else: print("Draw")
This is the best ans :
for i in range(n): if string[i] in vowels: kevin += n - i else: stuart += n - i
if stuart > kevin: print("Stuart", stuart) elif kevin > stuart: print("Kevin", kevin) else: print("Draw")
The Minion Game – Efficient Approach
To solve The Minion Game, generating all substrings is not required and will cause performance issues for large strings.
The key idea is that each character contributes to multiple substrings based on its position:
If the character is a vowel, the score goes to Kevin
If it’s a consonant, the score goes to Stuart
The number of substrings starting at index i is len(string) - i
Using this logic, the problem can be solved in O(N) time with a single loop.
def minion_game(string): vowels = "AEIOU" kevin = 0 stuart = 0 n = len(string)
This approach avoids substring creation and works efficiently for all test cases.
def minion_game(string): # your code goes here stuart_score = 0 kevin_score = 0 score = 0 vowels = 'aeiou' for i in range(len(string)): score = len(string) - i if string[i].lower() in vowels: kevin_score += score else: stuart_score += score
def minion_game(string): # your code goes here a = len(string) k = 0 s = 0 for i in range(a): if string[i].lower() in ['a','e','i','o','u']: k = a-i + k else: s = s+ a-i if s > k: print((f'Stuart {s}')) elif k>s: print((f'Kevin {k}')) else: print("Draw")
if name == 'main': s = input() minion_game(s)