+ 10

Hi guys i just want to know if it is possible to find the rank of a string. Is it possible?

Given a word all the letters of it are permuted in all possible ways and thus formed words are arranged as in a dictionary. Then the position of the given word is simply called rank of the word. Procedure to find the rank of a word Step 1: Write the word and above it,put the alphabetical ordering. Step 2:Write below,the factorial values starting with 0!(=1) from right to left. Step 3:Write the number of numbers less than(in top row) the number towards right of it. Step 4:Multiply the numbers shown by and add. Step 5:Add "1" to get the rank of the word. Example: The rank of the word "MASTER" is Step 1: 3 1 5 6 2 4 M A S T E R Step 2: 120 24 6 2 1 1 Step 3: 2 0 2 2 0 0 Step 4: 240+0+12+4+0+0=256 Step 5: 256+1=257 Therefore 257 is the rank of the word "MASTER"

4th Dec 2017, 4:03 PM
RR2001
RR2001 - avatar
10 Answers
+ 14
Marking so I can get back to this. This can be a fun exercise, and yes, it is perfectly possible. Characters are linked to their ASCII values.
3rd Dec 2017, 5:36 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
@Hatsy Rei Thanks.
3rd Dec 2017, 5:39 PM
RR2001
RR2001 - avatar
+ 4
Shamefully I only managed to get to step 1, failed to understand the numbers outputted on successive steps :| Good idea though, bookmarking the thread :)
4th Dec 2017, 3:54 AM
Ipang
+ 3
@RR2001, thanks! found one to read on quora just now, gonna read about it :)
4th Dec 2017, 1:49 PM
Ipang
+ 2
@lpang Its quite simple. Just google permutation and combination, how to find the rank of a word. That one was just an example
4th Dec 2017, 1:34 PM
RR2001
RR2001 - avatar
+ 2
@Gordie Now I get it! Thanks! Ill simply use this to get the count: for(int i=0;i<str.size();i++) { ctr[i] = count_if(a.begin()+1,a.end(), [](char c){return c<a[i];}) }
4th Dec 2017, 2:16 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
There is one more method to find the rank but before that you need to find the factorial of a number. EX, factorial of 5 is , 5! = 5 x 4 x 3x 2 x 1 =120, Now the question would be find the dictionary rank of the word LIFT ANS First of all arrange in alphabetical order, F I L T Now no of words beginning with F (I,L,T) - 3! = 6 no of words beginning with I (F,L,T) - 3! = 6 since L is the first letter of the word whose rank is what we are concerned L is locked and the our second unknown letter will be F L x x x No of words beginning with F = 2! = 2 I is the second letter of the word whose rank is what we are concerned so I is also locked L I x x if proceed in the original order the subsequent letter is F, so lock F L I F x Next letter finally turns out to be T L I F T. so add total = 6 + 6+ 2 = 14 as lift is also a word we need to consider that too 14 + 1 = 15. Rank of lift is 15. While codes on this topic are welcome, from my side i am working on this method, as this method needs loops to work out. Interested coders can try this method and the other method and also write a code for finding the rank of a word having repeating letters(not distinct). The concept varies for this code and i recommend you to watch a video since it is hard to explain via words
4th Dec 2017, 4:01 PM
RR2001
RR2001 - avatar
+ 1
What do we have to do in step 3? value[pos]-value[pos+1]? Can someone explain me step 3 if possible?
4th Dec 2017, 1:45 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
4th Dec 2017, 8:29 PM
Venkat Subramanian
Venkat Subramanian - avatar
- 1
yes
5th Dec 2017, 2:38 PM
Pintu Bhardwaj
Pintu Bhardwaj - avatar