Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:30:31+00:00 2026-06-07T16:30:31+00:00

The problem at hand is: Given a string. Tell its rank among all its

  • 0

The problem at hand is:

Given a string. Tell its rank among all its permutations sorted
lexicographically.

The question can be attempted mathematically, but I was wondering if there was some other algorithmic method to calculate it ?

Also if we have to store all the string permutations rankwise , how can we generate them efficiently (and what would be the complexity) . What would be a good data structure for storing the permutations and which is also efficient for retrieval?

EDIT

Thanks for the detailed answers on the permutations generation part, could someone also suggest a good data structure? I have only been able to think of trie tree.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:30:32+00:00Added an answer on June 7, 2026 at 4:30 pm

    There is an O(n|Σ|) algorithm to find the rank of a string of length n in the list of its permutations. Here, Σ is the alphabet.

    Algorithm

    Every permutation which is ranked below s can be written uniquely in the form pcx; where:

    • p is a proper prefix of s
    • c is a character ranked below the character appearing just after p in s. And c is also a character occurring in the part of s not included in p.
    • x is any permutation of the remaining characters occurring in s; i.e. not included in p or c.

    We can count the permutations included in each of these classes by iterating through each prefix of s in increasing order of length, while maintaining the frequency of the characters appearing in the remaining part of s, as well as the number of permutations x represents. The details are left to the reader.

    This is assuming the arithmetic operations involved take constant time; which it wont; since the numbers involved can have nlog|Σ| digits. With this consideration, the algorithm will run in O(n2 log|Σ| log(nlog|Σ|)). Since we can add, subtract, multiply and divide two d-digit numbers in O(dlogd).

    C++ Implementation

    typedef long long int lli;
    
    lli rank(string s){
        int n = s.length();
    
        vector<lli> factorial(n+1,1);
        for(int i = 1; i <= n; i++)
            factorial[i] = i * factorial[i-1];
        
        vector<int> freq(26);
        lli den = 1;
        lli ret = 0;
        for(int i = n-1; i >= 0; i--){
            int si = s[i]-'a';
            freq[si]++;
            den *= freq[si];
            for(int c = 0; c < si; c++) 
                if(freq[c] > 0) 
                    ret += factorial[n-i-1] / (den / freq[c]);
        }
        return ret + 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Could you give an example where static_assert(...) ('C++11') would solve the problem in hand
Hope someone will give me a hand with this problem I have. So here
I have a problem at hand, at first sight it looks easy and it
I'm thinking about how to approach the problem at hand. There's a flash movie
Problem : I have a hand held device that scans those graphic color barcodes
I have a problem that is getting embarrassingly out of hand. I'm working with
I tried my hand at this Google Codejam Africa problem (the contest is already
I have a problem in my android application, I have a hand page, I
i am trying to convert a bitmap to a base64 string.i can convert to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.