Answer:
Code written in Java below
Step-by-step explanation:
import java.util.HashSet;
import java.util.Set;
public class Test {// driver class
public static void main(String[] args) {
String[] rooms = {"+1A", "-1A", "+3F", "-3F", "+8X", "-8X"};
System.out.println("max room booked: "+solution(rooms));
}
public static String solution(String[] A) {
String ans ="";
int count=0, tempCount=0;
if(A.length == 1)
return A[0].substring(1, 3);//if the there is only one element in the array
Set<String> uniqueRooms = new HashSet<>();//for holding unqiue booked rooms
for(int i=0; i<A.length; i++) {
if(A[i].charAt(0) == '+') //set hold only booked rooms
uniqueRooms.add(A[i]);
}
for(String s: uniqueRooms) {
for(int i=0; i<A.length; i++) {
if(s.equals(A[i])) {
tempCount++;//count the booked rooms
}
}
if(tempCount>count) {//holding the max booked rooms
count = tempCount;
ans = s;
}
else if(count == tempCount) {//when more than one booked rooms have same count
if(s.charAt(1) < ans.charAt(1))
ans = s;
}
tempCount=0;
}
return ans.substring(1, 3);
}
}