# Hotel Rooms

Posted: 29 Jul, 2021

Difficulty: Moderate

#### You are the manager of a hotel having 10 floors numbered 0-9. Each floor has 26 rooms [A-Z]. You will be given a sequence of strings of the room where ‘+’ suggests the room is booked and ‘-’ suggests the room is freed. You have to find which room is booked the maximum number of times.

##### Note:

```
You may assume that the sequence is always correct, i.e., every booked room was previously free, and every freed room was previously booked.
In case, 2 rooms have been booked the same number of times, you have to return Lexographically smaller room.
A string 'a' is lexicographically smaller than a string 'b' (of the same length) if in the first position where 'a' and 'b' differ, string 'a' has a letter that appears earlier in the alphabet than the corresponding letter in string 'b'. For example, "abcd" is lexicographically smaller than "acbd" because the first position they differ in is at the second letter, and 'b' comes before 'c'.
```

##### For Example :

```
n = 6, Arr[] = {"+1A", "+3E", "-1A", "+4F", "+1A", "-3E"}
Now in this example room “1A” was booked 2 times which is the maximum number of times any room was booked. Hence the answer is “1A”.
```

##### Input Format :

```
The first line of input format contains ‘T’ denoting the number of test cases. Then each testcase follows
The first line of each test case contains an integer ‘n’ Denoting the number of times a room is booked or freed.
The second line of the test case contains an array of ‘n’ strings each string denoting which room was booked or freed.
```

##### Output Format :

```
For each test case, Print a string ‘ans’ denoting the room which was booked maximum number of times.
Output for every query will be printed in a separate line.
```

##### Note :

```
You are not required to print anything explicitly. It has already been taken care of. Just implement the functions.
```

##### Constraints :

```
1 <= T <= 10
1 <= N <= 10^4
Time Limit: 1 sec
```

Approach 1

The approach is simple, select a room from the sequence and count the total number of times that room was booked and check this for every room until we reach the end of list.

The steps are as following:

- Take a variable
**ans**in which we will store the final answer and a variable**maxCount**in which we will store maximum of number of times a room is booked. - Iterate through the array of strings from 0 to
**n**(say iterator be**i**):- Select the
**i**^{th}element of the array. - Take a variable
**count**which will store the count of the current element. - Iterate through the same array from 0 to n(say iterator be j):
- If the current string matches with the
**i**^{th}increase**count**by 1.

- If the current string matches with the
- If the
**count**of the current room is greater than**maxCount**update**maxCount**and ans. - Return
**ans**.

- Select the

Approach 2

In this approach, we will iterate through the array of strings and if we encounter a room that is booked then we will increase the count of the room by 1 and in the end return the room with a maximum number of bookings.

The steps are as follows:

- Take a map of strings
**m**which will store the count of every room. - Iterate through the array of strings from 0 to n(say iterator be i):
- If the room was booked increase the count of the current room.

- Take a variable
**ans**which will store the final answer and a variable**maxCount**which will store the maximum count of the room. - Iterate through the map
**m**and if we encounter any value greater than**maxCount**, update**maxCount**and**ans**. - Return
**ans**.

SIMILAR PROBLEMS

# Most Frequent Element

Posted: 25 Feb, 2022

Difficulty: Easy

# Shortest Common Supersequence

Posted: 4 Mar, 2022

Difficulty: Hard

# String Sort

Posted: 13 Apr, 2022

Difficulty: Easy

# Change Case

Posted: 14 Apr, 2022

Difficulty: Easy

# Reverse String

Posted: 15 Apr, 2022

Difficulty: Moderate