Solved it using Factorial number system


#1
int Solution::findRank(string A) {

long mod = 1000003;

long fac[A.length()+1];
fac[0] = 1;
fac[1] = 1;

for(int i = 2; i<= A.length() ; i++){
    fac[i] = (fac[i-1]*i)%mod;
}

string b = A;
sort(b.begin() , b.end());
unordered_map<char , int> pos;
for(int i = 0; i<b.length(); i++)pos[b[i]] = i;
vector<int> v;
for(int i = 0; i<A.length(); i++)v.push_back(pos[A[i]]);

for(int i = 0; i<v.size(); i++){
    for(int j = i+1; j<v.size(); j++){
        if(v[j] > v[i])v[j]--;
    }
}

reverse(v.begin() , v.end());

//for(int x : v) cout<<x<<' '; cout<<endl;

long n = 0;
for(int i = 0; i < v.size() ; i++){
    n = (n + v[i]*fac[i])%mod;
}
n++;
return (int)n;
}

#2

This solution won’t work…
we have to initialize the rank by 1 to get the correct answer.


#3

You are wrong . It’s an accepted solution. In factorial number system rank starts with 0.