Ninja has been learning about strings recently. While studying, he was fascinated by a pair of strings, say P and Q, in which every character in the first string, P, is less than the corresponding character in the second string, Q, i.e. P[i] < Q[i], where 0 <= 'i' < Length.
Now, he wants to count the number of such pairs of strings, of length 'N', which satisfies this rule.
Note:1. Strings contain only lower-case English alphabets.
2. Since the number of such pairs can be very large, return the answer modulo 10^9 + 7.
Example:
Letβs say length 'N' = 1. So, weβll consider only the strings with length 1: βaβ, βbβ, βcβ, β¦.., βzβ. Now, If P = βaβ, then Q can be βbβ, βcβ, βdβ....., βzβ (25 possibilities). If P = βbβ, then Q can be βcβ, βdβ....., βzβ (24 possibilities). And so on till P = βyβ, then Q can be βzβ (1 possibility).
Hence, the total number of pairs of strings that satisfy the given condition is: 25 + 24 + β¦β¦ + 1 = 325.
The first line of input contains an integer βTβ representing the number of test cases.
The first and the only line of every test case contains an integer βNβ representing the length of the pairs of strings to be found.
Output Format:
For each test case, return the number of pairs of strings that satisfy the given condition.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^8
Where βNβ is the length of the pairs of strings to be found.
Time limit: 1 sec
2
1
100
325
34244837
In test case 1, refer to the example explained above.
In test case 2, similarly, we can calculate for the string of length 100 as above logic in test case 1 and we get total valid pairs as 34244837.
2
2
1234567
105625
198263616
In test case 1, we consider only the string of length 2: βaaβ, βabβ,......, βazβ, βbaβ, βbbβ, β¦β¦, βbzβ,....., βzzβ.
Now, if P = βaaβ, then Q can be βbbβ, βbcβ,....., βbzβ, βcbβ, βcdβ....., βzzβ (625 possibilities).
Similarly, by calculating the number of possible pairs for the rest of the strings we get a total of 105625 possible pairs.
In test case 2, similarly, we can calculate for the string of length 1234567 as above logic in test case 1 and we get total valid pairs as 198263616.
Try generating all the possible strings of length N and then count the possible pairs.
A brute force approach could be to generate all the possible strings of length N and for each string, say P, we count the number of possible strings Q which satisfy the given condition. The idea is to start with an empty string and recursively append the characters to the string until we get a string of length 'N'. After generating a string, we iterate over it. For any character 'ch' present at index βiβ in the string βPβ (i.e. the generated string) βQ[i]' can store all the characters greater than βchβ (using the given condition) i.e. 'COUNT' = βzβ - 'ch'.
Multiplying the count value for every index, gives us the number of possible strings Q, for the generated string, 'P'.
Now, repeat the above procedure for the rest of the strings. The sum of all the count of possible strings Q, for all the generated strings, gives us the final answer.
O(26^N * N), Where βNβ is the length of strings.
Since we are generating all the possible strings of length N which takes O(26^N) time. For each string, we count the possible strings Q which takes O(N) time. Thus, the overall time complexity will be O(26^N * N).
O(N), Where βNβ is the length of strings.
Since O(N) extra space is required for storing the generated string. Also, O(26) i.e. constant space is required by the recursion stack. Thus the overall space complexity is O(N).