Rank The Permutations | Practice | GeeksforGeeks

Given a string, S find the rank of the string amongst all its permutations sorted lexicographically.The rank can be big. So print it modulo 1000003. 
Note: Return 0 if the characters are repeated in the string.
Example 1:
Input: S = "abc"
Output: 1
Explaination: It is the smallest 
lexicographically string of the permutation.
Example 2:
Input: S = "acb"
Output: 2
Explaination: This is the second smallest
lexicographically string of the permutation.
Your Task:
You do not need to read input or print anything. Your task is to complete the function rank() which takes string S as input parameter and returns the rank modulo 1000003 of the string.
Expected Time Complexity: O(|S|2)
Expected Auxiliary Space: O(|S|)
Constraints:
1 ≤ |S| ≤ 15  
0
ravi1010prakash1 month ago
class Solution{public:const int CHAR=256; int fact(int n) {    return (n <= 1) ? 1 : n * fact(n - 1); } int rank(string str) {       int res = 1;    int n=str.length();   long long mul= fact(n);   int count[CHAR]={0};   for(int i=0;i<n;i++)       count[str[i]]++;        for(int i=0;i<n;i++)        if(count[str[i]]>1)        return 0;   for(int i=1;i<CHAR;i++)       count[i]+=count[i-1];   for(int i=0;i<n-1;i++){       mul=mul/(n-i);       res=res+count[str[i]-1]*mul;       for(int j=str[i];j<CHAR;j++)           count[j]--;   }   return res%1000003;    }};
+1
sknwd88641 month ago
Time Complexity : O(N * Constant)
Space Complexity : O(N)
    long long fact[16];

    long long mod = 1000003;

    void find_factorial()
    {
       fact[0] = 1;
    
       fact[1] = 1;
    
       for(int i = 2; i <= 15; i++)
       {
           fact[i] = i * fact[i - 1];
       }
    }

    int rank(string str){
        
        int n = str.size();
        
        unordered_map<int, int> mp;
        
        for(int i = 0; i < n; i++)
        {
            mp[str[i]]++;
        }
        
        for(auto x : mp)
        {
            if(x.second > 1)
            {
                return 0;
            }
        }
        
        find_factorial();
        
        int x = n - 1;
        
        vector<int> freq(256, 0);
        
        for(int i = 0; i < n; i++)
        {
            freq[str[i]]++;
        }
        
        long long ans = 0;
        
        for(int i = 0; i < n; i++)
        {
            int count = 0;
            
            for(int j = 0; j < 256; j++)
            {
                if(str[i] == j)
                {
                    break;
                }
                else if(freq[j])
                {
                    count++;
                }
            }
            
            freq[str[i]] = 0;
            
            long long factorial = fact[x];
            
            x--;
            
            ans = (ans % mod + (count % mod * factorial % mod)) % mod;
        }
        
        return ans + 1;
    }
+1
sknwd88641 month ago
Time Complexity : O(N * N)
Space Complexity : O(N)
    long long fact[16];

    long long mod = 1000003;

    void find_factorial()
    {
       fact[0] = 1;
    
       fact[1] = 1;
    
       for(int i = 2; i <= 15; i++)
       {
           fact[i] = i * fact[i - 1];
       }
    }

    int rank(string str){
        
        int n = str.size();
        
        unordered_map<int, int> mp;
        
        for(int i = 0; i < n; i++)
        {
            mp[str[i]]++;
        }
        
        for(auto x : mp)
        {
            if(x.second > 1)
            {
                return 0;
            }
        }
        
        find_factorial();
        
        int x = n - 1;
        
        long long ans = 0;
        
        for(int i = 0; i < n; i++)
        {
            int count = 0;
            
            for(int j = i + 1; j < n; j++)
            {
                if(str[j] < str[i])
                {
                    count++;
                }
            }
            
            long long factorial = fact[x];
            
            x--;
            
            ans = (ans % mod + (count % mod * factorial % mod)) % mod;
        }
        
        return ans + 1;
    }
0
vinaysarda28122 months ago
int fact(int n){       if(n == 1 || n==0){           return 1;       }       return n * fact(n-1);   }   int getRank(string s, char c){       int count = 0;       for(char i : s){           if(i<c)count++;       }       return count;   }   string substring(string S,int s, int e){       string ans = "";       for(int i=s;i<e;i++){           ans.push_back(S[i]);       }       return ans;   }   int rank(string S){       // code here       int n = S.size() - 1;       int gap = fact(n);              int ans = 0;       unordered_map<char, int> mp;              for(int i=0;i<S.size();i++){           if(mp[S[i]] == 1){               return 0;           }           mp[S[i]]++;       }              for(int i=0;i<S.size();i++){           string str = substring(S,i, S.size());           int curr_rank = getRank(str, S[i]);           // cout<<curr_rank<<endl;           ans = (ans + (curr_rank * gap)%mod)%mod;           if(n){               gap = gap / n;               n--;           }       }       return (ans + 1)%mod;          }
0
arushi10232 months ago
Mobile
0
sachinupreti1902 months ago
EASY CODE IN C++.
// BY SLIETIAN
 
HERE WE GO:--
 
class Solution{public:   int rank(string S){       // code here       unordered_map<char,int>m1;       int i,c=0;       for(i=0;i<S.size();i++)       {           m1[S[i]]++;       }       unordered_map<char,int>::iterator it;       for(it=m1.begin();it!=m1.end();++it)       {           if(it->second > 1)           {               c++;               break;           }       }        if(c>0)        return 0;        else        {            int d=1;            string k=S;            sort(k.begin(),k.end());            while(k!=S)            {                next_permutation(k.begin(),k.end());                d++;                  d=d%1000003;            }            return d%10000003;                   }   }}; 
+1
purushottamkumar42 months ago
Simple C++ Solution : 0.4s
 
int rank(string S)
{    
	string k =S;
    sort(k.begin() , k.end());
       
    unordered_set<char>s;
    for(char ch:k) s.insert(ch);
    
    if(S.size()!=s.size()) return 0;
      
    int ans=1;
    while(k!=S)
    {
       next_permutation(k.begin() , k.end());
       ans++;
       ans=ans%1000003; 
    }
    return ans%1000003;
}
0
taiphanvan24033 months ago
//C++:0s
 bool repeated(string str){
        sort(str.begin(),str.end());
        for(int i=0;i<str.size()-1;i++){
            if(str[i]==str[i+1]) return true;
        }
        return false;
    }
    int giaithua(int n)
    {
        if(n==0 || n==1) return 1;
        return n*giaithua(n-1);
    }
    int rank(string str)
    {
        if(repeated(str)) return 0;
    	int Empty = str.size() - 1;
    	string tam = str;
	    sort(tam.begin(),tam.end());
	    unordered_map<char, pair<int,int>>M;
    	for (int i = 0; i < tam.size(); i++)
	   	    M[tam[i]] = { i,i };
	    long long sum = 1;
    	int i = 0;
	    int n = str.size();
	    int Gt = giaithua(Empty);
    	while (Empty != 0)
	    {
		    int vt = M[str[i]].second;
		    sum += vt *1ll* Gt;
		    Gt /= Empty;
		    sum%=mod;
	    	Empty--;
		    for (int k = M[str[i]].first+1; k < n; k++)
		    	M[tam[k]].second--;
		    i++;
	    }
	    return sum%mod;
    }
0
taiphanvan2403
This comment was deleted.
+1
kake13373 months ago
   long long getfact(long long n)
   {
       return (n==1)?1:(n*getfact(n-1));
   }
   int rank(string S){
       int n=S.length();
       vector<int> v(27,0);
       for(int i=0;i<n;i++)
       {
           v[S[i]-'a'+1]++;
           if(v[S[i]-'a'+1]>1)
           return 0;
       }
       for(int i=2;i<27;i++)
       v[i]+=v[i-1];
       long long f=getfact(n);
       long long r=1;
       for(int i=0;i<n;i++)
       {
           f/=(n-i);
           r+=f*v[S[i]-'a'];
           for(int j=S[i]-'a'+1;j<27;j++)
           v[j]--;
       }
       return r%1000003;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.