Saturday 18 April 2020

Hackerearth : Roy's birthday and balloons problem : Solution in Python






PROBLEM STATEMENT
Points: 30
Its Roy's birthday and he is decorating his house with balloons.
He has 26 different types of balloons. They are represented by [a-z].
Now Roy does not want to have few particular balloons together. Actually he doesn't want to keep any of the two vowel (i.e [a,e,i,o,u] ) balloons together.
So, in one arrangement no two vowels should be together. Given the string of balloons there can be many such arrangements possible.
Help Roy in finding the number of possible arrangements.
Number of such arrangements can be quite large, so print it modulo 1000000007 (109 + 7)
Also, there's a chance that no such arrangement is possible. Print "-1" in such cases (without quotes)
Input:
First line contains T, number of test cases.
Each of next T lines contains a string composed of lower case letters representing all the balloons.
Output:
For each test case print the required answer.
Constraints:
1 <= T <= 100
1 <= Length of string <= 1000000 (106)
Note: Sum of string lengths over all the test cases in one file does not exceed 1000000 (106)
SAMPLE INPUT
 
2
abe
ae
SAMPLE OUTPUT
 
2
-1
Explanation
Test #1: Possible arrangements are [1] abe [2] eba
Test #2: There are no any arrangements possible such that any two vowels are not together. So print -1.
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded if any testcase passes.


Solution in Python :


import math
import array
total = array.array('i',[])
i = 0
t = int(input())
while i<t :
    count = 0;
    s = input()
    for j in s :
        if j == 'a' or j == 'e' or j == 'i' or j == 'o' or j == 'u' :
           count+=1
    cons = len(s) - count
    factcons = math.factorial(cons)
    factvowl = math.factorial(count)
    factdiff = math.factorial(abs(cons-count))
    total.append(int(factcons*factvowl*(cons+1)*factcons/(factvowl*factdiff)))
    i+=1
for ele in total :
   print(ele)

1 comment: