An automated classroom assessment system determines a class's overall performance based on a string of individual student grades. The grades are 'A', 'B', 'C', 'D', 'E', 'F', with a strict precedence order: A > B > C > D > E > F.
The final grade is determined by comparing the total count of high-ranking grades against the total count of low-ranking grades.
1) High Group: Consists of grades 'A', 'B', and 'C'.
2) Low Group: Consists of grades 'D', 'E', and 'F'.
First, calculate the total score for each group:
high_score = count('A') + count('B') + count('C')
low_score = count('D') + count('E') + count('F')
The final dominant grade is determined by the following rules:
If high_score > low_score, the winner is the highest-ranking grade that is present in the High Group (check for 'A' first, then 'B', then 'C').
If low_score > high_score, the winner is the highest-ranking grade that is present in the Low Group (check for 'D' first, then 'E', then 'F').
If high_score == low_score (a tie), the winner is the highest-ranking grade present in the entire class.
Your task is to implement a function that takes the string of grades and returns the single character representing the final dominant grade.
The first line contains a single integer T, the number of test cases.
Each of the next T lines contains a single string grades.
For each test case, print a single character representing the final dominant grade on a new line.
3
AAABBC
ABFFFF
BCDE
A
F
B
Test Case 1 (AAABBC): High group ('A','B','C') count = 3+2+1=6. Low group count = 0. High group wins. The highest grade present in the high group is 'A'.
Test Case 2 (ABFFFF): High group ('A','B') count = 1+1=2. Low group ('F') count = 4. Low group wins. The highest grade present in the low group is 'F'.
Test Case 3 (BCDE): High group ('B','C') count = 1+1=2. Low group ('D','E') count = 1+1=2. It's a tie. The highest grade present in the entire class is 'B'.
The expected time complexity is O(N).
1 <= T <= 1000
1 <= length of grades string <= 10^5
The total length of all grade strings over all test cases will not exceed 5 * 10^5.