Instagram
youtube
Facebook

The Minion Game Hackerrank Solution

Mradul Mishra
Table of Contents

Kevin and Stuart want to play the 'The Minion Game'. Game Rules Both players are given the same string, S. Both players have to make substrings using the letters of the string S.

Kevin and Stuart want to play the 'The Minion Game'.

Game Rules

Both players are given the same string, S.
Both players have to make substrings using the letters of the string S.
Stuart has to make words starting with consonants.
Kevin has to make words starting with vowels.
The game ends when both players have made all possible substrings.

Scoring
A player gets +1 point for each occurrence of the substring in the string S.

For Example:
String  = BANANA
Kevin's vowel beginning word = ANA
Here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points.

For a better understanding, see the image below:

miniongameexample

 

 

 

 

 

 

 

 

 

 

 

Solution 

def minion_game(string):
    # your code goes here
    n = len(string)
    comb = ((n)*(n+1))/2
    count_k = 0
    count_s = 0
    count_k = sum([len(string[i:]) for i in range(len(string)) if string[i] in "AEIOU"])
    count_s = comb - count_k
    
    if count_s == count_k:
        print("Draw")
    elif count_s > count_k:
        print("Stuart", int(count_s) )
    else:
        print("Kevin", int(count_k))

if __name__ == '__main__':
    s = input()
    minion_game(s)

 

Steps Used to solve the problem:

  1. To get the total number of substrings in a string we have used the formula comb = ((n)*(n+1))/2.
  2. After this we used list comprehension to check for all the words which are having vowels.
  3. For each vowel letter the combination of its words would be the number of words that are coming after that, which could be easily found using len(string[i:]).
  4. Once we found all the combinations of words that are vowels, we did the sum of them and stored them in the variable count_k.
  5. To find the count of Stuart we did subtract that with a total number of combinations using count_s = comb - count_k.

 

This was the solution for 'The Minion Game'. A very popular coding problem on Hackerrank.com

If you still face any problem with this solution, please do comment down below.

Add a comment:

Comments:

Swati Rani

Your code did not execute within the time limits. Please optimize your code. For more information on execution time limits, refer to the environment page

Harsh Tripathi

c

Shaurya

You do not need to read any input in this challenge. Output Format Print Hello, World! to stdout. Sample Output 0 Hello, World!