DEV Community

Cover image for The Minion Game - HackerRank Solution Python
Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

2

The Minion Game - HackerRank Solution Python

The Minion Game is an interesting medium level problem in Python. In this post, we will see the solution of The Minion Game in Python.

Problem Statement and Explanation

Kevin and Stuart have decided to play a game called ‘The Minion Game’. The game has some rules that they both must follow.

Here are the rules:

  • Both players will receive the exact same string.
  • Using the letters from the string, both players must create as many substrings as possible.
  • Stuart can only create words that start with consonants.
  • Kevin can only create words that start with vowels.
  • The game ends when both players have created all possible substrings.

Input Format

  • A single line of input containing the string S.

Output Format

Return one of the following strings based on the winner:

  • If Stuart wins, return Stuart followed by the score separated by a space.
  • If Kevin wins, return Kevin followed by the score separated by a space.

The Minion Game Python Solution

# The Minion Game - HackerRank Solution Python
def minion_game(string):
# your code goes here
vowels = 'AEIOU'
stuart_score = 0
kevin_score = 0
for i in range(len(string)):
if string[i] in vowels:
kevin_score += len(string) - i
else:
stuart_score += len(string) - i
if stuart_score > kevin_score:
print('Stuart', stuart_score)
elif stuart_score < kevin_score:
print('Kevin', kevin_score)
else:
print('Draw')
if __name__ == '__main__':
s = input()
minion_game(s)

Explanation of Solution

  • The function first defines a list of vowels, vowels.
  • It then declares two variables, stuart_score and kevin_score, to keep track of the scores of the two players.
  • The for loop iterates over the characters in the string.
  • In each iteration, the function checks if the current character is a vowel.
  • If it is, the function adds the length of the string minus the index of the character to kevin_score.
  • Otherwise, the function adds the length of the string minus the index of the character to stuart_score.
  • The if statement checks which player has the higher score.
  • If stuart_score is higher, the function prints Stuart followed by the value of stuart_score.
  • If kevin_score is higher, the function prints Kevin followed by the value of kevin_score.
  • If the scores are equal, the function prints Draw.

Time Complexity of the Solution

The for loop in the minion_game function iterates over the characters in the string. The number of characters in the string is n, so the for loop takes n iterations. Each iteration takes constant time, so the overall time complexity of the function is O(n).

Space Complexity of the Solution

The minion_game function only uses two variables, stuart_score and kevin_score. These variables are of constant size, so the overall space complexity of the function is O(1).

Problem statement is taken from Hackerrank, and the solutions are implemented by CodePerfectPlus team

Other Article By Author

30 Days of Code SubReddit

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay