-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedPermutationRank.cpp
More file actions
46 lines (46 loc) · 926 Bytes
/
SortedPermutationRank.cpp
File metadata and controls
46 lines (46 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*Question:
Sorted Permutation Rank
Asked in:
Housing
Zenefits
Microsoft
Given a string, find the rank of the string amongst its permutations sorted lexicographically.
Assume that no characters are repeated.*/
int* returnfact(int n,int m)
{
int *a=new int[n+1];
int w=1;
for(int i=1;i<=n;i++)
{
w=((w%m)*(i%m))%m;
a[i]=w;
}
return a;
}
int Solution::findRank(string b) {
int *a=new int[256];
for(int i=0;i<256;i++)
a[i]=0;
for(int i=0;i<b.size();i++)
{
a[b[i]]++;
}
int ans=0;
int count=0;
int mod=1000003;
int *fact=returnfact(b.size(),mod);
for(int i=0;i<b.size();i++)
{
int lim=b[i];
count=0;
for(int i=0;i<lim;i++)
{
count+=a[i];
}
ans=(ans%mod+(count*fact[b.size()-i-1])%mod)%mod;
count=0;
a[lim]--;
//cout<<ans;
}
return ans+1;
}