Simple c++ solution using dictionary concept


#1

int fact(int n)
{
if(n==0)
return 1;
int fac=1;
while(n>=1)
{
fac=((fac% 1000003)*n)% 1000003;
n=n-1;
}
return fac% 1000003;
}
int Solution::findRank(string A)
{
string cpy=A;
bool visited[A.length()-1];
memset(visited,false,sizeof(visited));
int n=A.length()-1;
sort(cpy.begin(),cpy.end());
int i=0;
int j=0;
int cnt=0;
while(i<=n)
{
if(A[i]!=cpy[j] && visited[j]==false)
{
cnt=(cnt% 1000003+fact(n-i)% 1000003)% 1000003;
//cout<<cnt<<" ";
j++;
}
else if(A[i]==cpy[j])
{
visited[j]=true;
i=i+1;
j=0;
}
else
{
j=j+1;
}
}
return (cnt+1)% 1000003;
}