Rank The Permutations
Submissions: 4844   Accuracy:

22.3%

  Difficulty: Medium   Marks: 4

Given a string, find the rank of the string amongst its permutations sorted lexicographically. 
Return 0 if the characters are repeated in the string.

For example:

Input : 'acb'
Output : 2

The order permutations with letters 'a', 'c', and 'b' : 
abc
acb
bac
bca
cab
cba

The answer might not fit in an integer, so return your answer % 1000003

Input:

The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a separate line.


Output:

Print the rank of the string amongst its permutations.

Constraints:

1 ≤ T ≤ 50

1 ≤ |s| ≤ 15

Example:

Input:
2
abc
acb
Output:
1
2

** For More Input/Output Examples Use 'Expected Output' option **

Author: shef5


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.