![]() |
VOOZH | about |
Given an array of strings votes[], representing the voters' rankings. A special ranking system where each voter ranks all teams from highest to lowest. The task is to Sort all teams according to the following ranking system and return a string of all the teams sorted by the ranking system.
Example:
Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
Output: "ACB"
Explanation:
- Team A was ranked first place by 5 voters. No other team was voted as first place, so team A is the first team.
- Team B was ranked second by 2 voters and ranked third by 3 voters.
- Team C was ranked second by 3 voters and ranked third by 2 voters.
- As most of the voters ranked C second, team C is the second team, and team B is the third.
Input: votes = ["AXYZ","XYZA"]
Output: "XWYZ"
Explanation:X is the winner due to the tie-breaking rule. X has the same votes as A for the first position, but X has one vote in the second position, while A does not have any votes in the second position.
Approach:
Create an array which consists of the characters present in the votes array . Now we need to sort this array on the basis of votes given by all the members. Hence i will create a map of type <char,int> which will store the count of votes given to a character for a particular position
For example:mpp[{'a' , 2}] represents the count of vote given to character a for the second position
This data will help us in lamba function to sort the array . We have to first compare the two characters on the basis of votes at a particular position from 1 till votes[0].size() and thereafter on the basis of the alphabetical order of the characters
Finally when the array is sorted successfully we will then append the characters of the array in a string (in same order) and return the answer
Steps-by-step approach:
Below is the implementation of the above approach:
ACB
Time complexity: O(N M*logn), where N is the number of unique characters in the votes array and M is number of people giving the votes
Auxiliary Space: O(N*M), where N is the number of unique characters in the votes array and M is number of people giving the votes