There are 10 floors in a hotel (numbered from 0 to 9). On each floor there are 26 rooms, each marked with a capital letter of the English alphabet (from "A" to "Z"). Your task is to compute which room was booked most frequently, based on a list of room reservations. The list of reservations consists of N three-character strings. The first character of the string is "+" or "−", which describes whether the room was booked or freed. The second and third characters correspond to the number of the floor and letter of the room, respectively. For example "+4C" means that room C on the 4th floor has just been booked, and "−0G" means that room G on the 0th floor has been freed. You may assume that the list describes a correct sequence of bookings in chronological order; that is, only free rooms can be booked and only booked rooms can be freed. All rooms are initially free. Note that this doesn't mean that all rooms have to be free at the end.Write a function: class Solution { public String solution(String[] A); } that, given an array A consisting of N strings, representing the list of bookings, returns a two-character string consisting the floor number and letter of the room that was booked the most times. It is possible that more than one room might have been booked the same, maximum number of times; in this case, return the one whose identifier is the smallest alpha-numerically. Please write in Java. Thanks!Examples: Given A = ["+1A", "−1A", "+3F", "−3F", "+3F", "+8X"], your function should return "3F". Room 3F was booked twice, while rooms 1A and 8X were booked only once. Note that rooms 3F and 8X are still booked at the end. Given A = ["+1A", "+3F", "+8X", "−1A", "−3F", "−8X"], your function should return "1A". All of the rooms "1A", "3F" and "8X" were booked once. "1A" is the smallest alpha-numerically out of them. Given A = ["+0A"], your function should return "0A". Given A = ["+9Z", "−9Z", "+9Z", "−9Z", "+9Z", "+3B"], your function should return "9Z", as room 9Z was booked three times. Assume that: N is an integer within the range [1..600]; each element of array A is a string consisting of three characters: "+" or "−", a digit ("0"-"9"), and uppercase English letter ("A"-"Z"); the sequence is correct, that is every booked room was previously free and every freed room was previously booked. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Respuesta :

Answer:

Code written in Java below

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);

  }

}

ACCESS MORE