Definition
The rank of a word is defined as the position of the word when all the possible permutations (or arrangements) of its letters are arranged alphabetically, like in a dictionary, no matter if the words are meaningful or not.
Let us consider these two words - "blue" and "seen". To begin with, we would write all the possible arrangements of the letters of these words in alphabetical order:
"blue": "belu","beul","bleu","blue","buel","bule","eblu","ebul","elub","elbu","eubl",
"eulb","lbeu","lbue","lebu","leub","lube","lueb","ubel","uble","uebl","uelb",
"ulbe","uleb"
"seen": "eens","eesn","enes","ense","esen","esne","nees","nese","nsee","seen",
"sene","snee"
Now let's look from the left and find the position of the words we need. We see that the word "blue" is at the 4th position and "seen" is at 10th position. So the rank of the word "blue" is 4, and that of "seen" is 10. This is the general way of calculating the rank of a word. Make sure you start counting from 1 only.
Task
Your task is to write a code to take any word as an input and display its rank. The rank should be the output. Be careful about words containing repeated letters.
Examples
"prime" -> 94
"super" -> 93
"bless" -> 4
"speech" -> 354
"earth" -> 28
"a" -> 1
"abcd" -> 1
"baa" -> 3
You can assume the input to be completely in lowercase and the input will only contain alphabetical characters. Also if a blank space or an invalid string is entered, you may return anything.
Scoring
This is code-golf , so the shortest code wins!
O(n log n)
or less. (sorry, no Python) My submission (C++) takes 2.53s to solve test 14. \$\endgroup\$['h', 'e', 'l', 'l', 'o']
as opposed to'hello'
? \$\endgroup\$