forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexicographic_rank.c
70 lines (61 loc) · 1.34 KB
/
Lexicographic_rank.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// To find the rank of a string among all its lexicographically sorted permutations.
#include <stdio.h>
#include <string.h>
int lexicographic_rank(char*);
int fact(int);
int smallerright(char *, int);
int main()
{
char string[100];
printf("Enter the string:");
scanf("%s", string);
printf("Lexicographic Rank of the string=%d", lexicographic_rank(string));
}
// Function to calculate the rank of the string
int lexicographic_rank(char *string)
{
int length = strlen(string);
int total_permutation = fact(length);
int i = 0;
int rank = 1;
int countsmallright;
while (*(string + i) != '\0')
{
total_permutation = total_permutation / (length - i);
countsmallright = smallerright(string, i);
rank = rank + (countsmallright *total_permutation);
i++;
}
return rank;
}
// Function to check if the element on the right side is smaller than the index ith element
int smallerright(char *string, int index)
{
int i = index;
int count = 0;
while (*(string + i) != '\0')
{
if (*(string + i)<*(string + index))
{
count = count + 1;
}
i++;
}
return count;
}
// To calculate factorial of a number using recursion
int fact(int num)
{
if (num == 0)
return 1;
else
return (num* fact(num - 1));
}
/*
Sample Output
Enter the string:rohan
Lexicographic Rank of the string=117
Complexities
Time Complexity:O(n^2)
Space Complexity:O(n)
*/