Leaderboard
Showing:
Rank The Permutations
Medium Accuracy: 29.2% Submissions: 8116 Points: 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

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Rank The Permutations

Output Window