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
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
- HackerRank Solve Python Algorithm Series
- HackerRank Problem Solving Algorithm Series
- 30 Days of Code HackerRank
- 10 Days of JavaScript HackerRank
Top comments (0)