23
1
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!
Related. – Martin Ender – 2018-01-18T17:55:50.947
14"Make sure you start counting from 1 only." - It's totally up to you to have this requirement, but do note that it's quite common to allow either 0 or 1 based indexing for such challenges. – Jonathan Allan – 2018-01-18T18:21:50.303
1Yeah ikr but if you start from 0 then you actually aren't displaying the original rank which is why I decided to add this requirement. – Manish Kundu – 2018-01-18T19:39:35.530
Useful link. You will get AC if your program runs in time
O(n log n)
or less. (sorry, no Python) My submission (C++) takes 2.53s to solve test 14. – user202729 – 2018-01-19T02:20:43.473Can I do a tuple or a list with that word, e.g.
['h', 'e', 'l', 'l', 'o']
as opposed to'hello'
? – 0WJYxW9FMN – 2018-01-20T11:55:02.787@user202729 AC? – haneefmubarak – 2018-01-20T12:46:48.190
@haneefmubarak "accepted". Commonly-known verdict on competitive programming online judge sites. – user202729 – 2018-01-20T12:47:49.947