The Minion Game - Python HackerRank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Python The Minion Game Problem with a very easy explanation. In this article, you will get more than one approach to solve this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Python The Minion Game Solution - Problem Statement
Kevin and Stuart want to play '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 S = BANANA
Kevin's vowel beginning word = ANA
Here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points.
For better understanding, see the image below:
Source: The Minion Game Python HackerRank |
Your task is to determine the winner of the game and their score.
Function Description
- Complete the minion_game in the editor below.
- minion_game has the following parameters:
- string: the string to analyse
Prints
- string: the winner's name and score, separated by a space on one line, or Draw if there is no winner
Input Format
A single line of input containing the string S.
Note: The string S will contain only uppercase letters: [A - Z].
Constraints
3 < len(s) <= 10^6
Sample Input 0
BANANA {codeBox}
Sample Output 0
Stuart 12 {codeBox}
Note
Vowels are only defined as AEIOU. In this problem, Y is not considered a vowel.
Python The Minion Game - Hint to Solve the question
This challenge can be solved by findings all the substrings of the string and separating the substrings on the basis of their first character.
If the string is BANANA, the substrings are:
B
BA
BAN
BANA
BANAN
BANANA
A
AN
ANA
ANAN
ANANA
N
NA
NAN
NANA
A
AN
ANA
N
NA
A
It is interesting to note that in BANANA:
Words formed using the first letter B = 6
Words formed using the second letter A = 5
Words formed using the third letter N = 4
Words formed using the fourth letter A = 3
Words formed using the fifth letter N = 2
Words formed using the last letter A = 1
Using this pattern, you can run a loop from the start to the end of the string and filter out words starting with vowels and consonants.
Python The Minion Game - Hacker Rank Solution
Approach I: The Minion Game HackerRank Python Solution
# ========================
# Information
# ========================
# Name: The Minion Game - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/the-minion-game/problem
# Difficulty: Medium
# Max Score: 40
# Language: Python 3
# ========================
# Solution Start
# ========================
# The Minion Game Python - Hacker Rank Solution
def minion_game(string):
# your code goes here
vowels = 'AEIOU'
str_lenght = len(string)
kevin_score, stuart_score = 0, 0
for i in range(str_lenght):
if s[i] in vowels:
kevin_score += (str_lenght - i)
else:
stuart_score += (str_lenght - i)
if kevin_score > stuart_score:
print("Kevin", kevin_score)
elif kevin_score < stuart_score:
print("Stuart", stuart_score)
else:
print("Draw")
#your code ends here
if __name__ == '__main__':
s = input()
minion_game(s)
# The Minion Game in Python - Hacker Rank Solution END
# MyEduWaves
Approach II: The Minion Game HackerRank Python Solution
# ========================
# Information
# ========================
# Name: The Minion Game - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/the-minion-game/problem
# Difficulty: Medium
# Max Score: 40
# Language: Python 3
# ========================
# Solution Start
# ========================
# The Minion Game Python - Hacker Rank Solution
def minion_game(string):
# your code goes here
player1 = 0;
player2 = 0;
str_len = len(string)
for i in range(str_len):
if s[i] in "AEIOU":
player1 += (str_len)-i
else :
player2 += (str_len)-i
if player1 > player2:
print("Kevin", player1)
elif player1 < player2:
print("Stuart",player2)
elif player1 == player2:
print("Draw")
else :
print("Draw")
#your code ends here
if __name__ == '__main__':
s = input()
minion_game(s)
# The Minion Game in Python - Hacker Rank Solution END
# MyEduWaves
Disclaimer: The above Problem ( Python The Minion Game ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python The Minion Game Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Python