Last Updated: 31 Mar, 2025

Team Game

Easy

Problem statement

You are given an array of characters 'G' of length 'N', where each character represents the outcome of a game. The character 'A' indicates a win for Team A, and the character 'B' indicates a win for Team B.


Your task is to count the number of wins for each team and return the team that has won the most games. If both teams have won the same number of games, return "Tie".


For Example :
Let 'N' = 5, 'G' = ['A', 'B', 'A', 'A', 'B'].
Team A has won 3 games, and Team B has won 2 games.
Therefore, the answer is "A".
Input Format :
The first line contains an integer 'N', the number of games.
The second line contains the array of characters 'G' of length 'N', representing the game outcomes.
Output Format :
Return a string representing the team with the most wins ("A" or "B") or "Tie" if both teams have the same number of wins.
Note :
You don’t need to print anything. Just implement the given function.
Constraints :
1 <= 'N' <= 10^5
Each character in 'G' will be either 'A' or 'B'.

Time Limit: 1 sec

Approaches

01 Approach

Approach:

  • Initialize two variables to store the win counts for Team A and Team B.
  • Iterate through the array of game outcomes.
  • For each game outcome, increment the win count of the corresponding team.
  • After iterating through all the games, compare the win counts of the two teams.
  • Return 'A' if Team A has more wins, 'B' if Team B has more wins, and 'Draw' if they have the same number of wins.

Algorithm:

  • Initialize a variable 'count_A' to 0.
  • Initialize a variable 'count_B' to 0.
  • Iterate using 'i' from 0 to 'N - 1':
    • If (G[i] == 'A'):
      • Increment 'count_A' by 1.
    • Else if (G[i] == 'B'):
      • Increment 'count_B' by 1.
  • If ('count_A' > 'count_B'):
    • Return 'A'.
  • Else if ('count_B' > 'count_A'):
    • Return 'B'.
  • Else:
    • Return 'Draw'.