You are not logged in. Please login at www.codechef.com to post your questions!

×

# PLSTR - Editorial

Author: Ayush Nagal

MEDIUM

Math

# PROBLEM:

Given a string $S$, find the rank of the string amongst its permutations sorted lexicographically. Assume that no characters are repeated.

# EXPLANATION:

It is a well-known problem of Permutation and Combination. First of all, we need an auxiliary array (like Counting Sort) to store the frequency of all the characters in the given string. It can be obtained as follows:

map <char,int> m;
map <char,int>::iterator it;

for(i=0;i < n;i++){
m[B[i]] = i;
}


After obtaining the auxiliary array, we can use the following code to obtain the rank:

while(fix < n)
{
r = m[A[fix]];
ans = ans + ((r)*fact(n-1-fix))%mod;
if(ans > mod)
ans = ans - mod;

for(it=m.begin();it!=m.end();it++)
{
if((*it).first>A[fix])
{
(*it).second -= 1;
}
}
fix++;
}


The rank will be $ans+1$.

# AUTHOR'S SOLUTION:

Author's solution can be found here.

This question is marked "community wiki".

asked 08 Mar, 14:08 3★ayush163
11
accept rate: 0% 19.8k350498541

 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,852
×2,657
×1,220
×9
×2

question asked: 08 Mar, 14:08

question was seen: 53 times

last updated: 14 Mar, 06:42