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
|
1246 Discussions
|
Please Login in order to post a comment
We can apply one final small optimisation in this problem, as we know that the sum of Stuart and Kevin's points for a given string is constant (equal to ((n + 1)n/2), where (n) is the length of the string). Therefore, we can skip keeping track of, for example, Stuart's points and, at the end, subtract Kevin's points from the known total.
def minion_game(string): vowel = 'AIUEO' kevin = 0 stuart = 0
My solution involves iterating over the string and check if the character in question is a consonant or a vowel. If the character is a vowel then the length of the substring from that character till the end of the string is to be added to kevin's last recorded score. This is because for each possible substring 1 point is awarded, so if you have a string 'abcd', here 'a', 'ab', 'abc' and 'abcd' substrings will go into kevin's favour, which means he will gain 4 points from all the substrings starting from 'a' and 4 is also the length of the substring starting from 'a' till the very end of the string. Similarly, while iterating over the string if the character is a constant then the length of the substring from the character till the end of the string is to be added to stuart's last known score. After the iteration is complete then the scores are compared and the appropriate result is displayed.