VOOZH about

URL: https://www.geeksforgeeks.org/dsa/special-ranking-teams-by-votes/

⇱ Special Ranking Teams by Votes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Special Ranking Teams by Votes

Last Updated : 23 Jul, 2025

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.

  • Teams are ordered by the number of position-one votes received.
  • If teams tie for the first position, the second position votes are used to resolve the tie, and this process continues until the tie is broken.
  • If a tie remains after considering all positions, the teams are ranked alphabetically.

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:

  • Initialize a set to store the unique characters (teams) and a vector to store the sorted teams.
  • Create a map to store the count of votes for each character (team) at each position.
  • Iterate through the votes[] array and update the set, array, and map accordingly.
    • Add the unique characters to the set and array.
    • Increment the count of votes for each character at each position in the map.
  • Sort the vector of characters based on the following criteria:
    • Compare the vote counts for each position. If the vote counts differ, sort based on the higher vote count.
    • If the vote counts are equal for all positions, sort the characters alphabetically.
  • Construct the final answer string by appending the characters in the sorted array.
  • Return the answer string.

Below is the implementation of the above approach:


Output
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


Comment