Sorted Permutation Rank InterviewBit Solution
- illuminati
- Sep 6, 2020
- 1 min read
Updated: Sep 17, 2020
Problem: Sorted Permutation Rank
Problem Description:
Given a string, find the rank of the string amongst its permutations sorted lexicographically. Assume that no characters are repeated.
Example :
Input : 'acb'
Output : 2
The order permutations with letters 'a', 'c', and 'b' :
abc
acb
bac
bca
cab
cba
The answer might not fit in an integer, so return your answer % 1000003.
Approach
Refer the problem Sorted Permutation Rank with Repeats. The approach & solution to the problem is exactly the same, except that we are not considering the frequency of character because, in this problem, characters are unique without repeat.
Time & Space Complexity
Time Complexity: O(N^2)
- For every index we are traversing the string, to find the number of character lesser than the current one.
Space Complexity: O(1)
- Apart from the space taken by our input array, we have taken the constant amount of space, independent of the size of the string.
Solution:
Code in C++
Recent Posts
See AllYou are given an array of N non-negative integers, A0, A1 ,…, AN-1.Considering each array element Ai as the edge length of some line segment
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Comentarios