Save the City
The City of Wonderland is under attack by an unknown virus. There are twenty six types of people who reside in the city of Wonderland i.e every person in the city belongs to any one of the types from a - z.
Many people have got infected by the virus and are fighting for their lives. Scientists have studied the virus and its impact on different types of people. After their study, they found that the virus is unable to cause damage to any of the people who belong to the type which are maximum in the city.
For example - If in the city, the maximum people belong to the type b, then all b types of people will be safe from the virus.
Scientists want to know the type for which the people are maximum so that they can safeguard them and also develop vaccination as an antidote to the virus which will provide immunity to other people. They are short of time and therefore need your help in determining the type for which the people are maximum in the city.
Note: If there are more than one type of people with equal maximum then the type with lesser ASCII value will be considered.
Can you help the Scientists in saving the city ?
Input Format
The first line of input consists of the number of test cases, T.
Next T lines each consists of a string representing the type of each individual person in the city.
Constraints
1<= T <=10
1<= |string| <=100000
Output Format
For each test case, print the required output in a separate line.
Sample TestCase 1
Explanation
Test Case 2: There are 2 y and 2 z types of people. Since the maximum value is same, the type with lesser Ascii value is considered as output. Therefore, y is the correct type.
Java Solutions
import java.io.*; import java.util.*; public class CandidateCode { static void characterCount(String inputString) { HashMapcharCountMap = new HashMap (); char[] strArray = inputString.toCharArray(); for (char c : strArray) { if (charCountMap.containsKey(c)) { charCountMap.put(c, charCountMap.get(c) + 1); } else { charCountMap.put(c, 1); } } int max = 0; for (Map.Entry entry : charCountMap.entrySet()) { int a = Integer.parseInt(String.valueOf(entry.getValue())); if(a > max) { max = a; } } for (Map.Entry entry : charCountMap.entrySet()) { int a = Integer.parseInt(String.valueOf(entry.getValue())); if(a == max) { System.out.println(entry.getKey() ); break; } } } public static void main(String args[] ) throws Exception { //Write code here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0 ; i < t ; i++) { String str = sc.next(); characterCount(str); } } }
0 Comments