Problem Link
Author: Bhuvnesh Jain
Tester: Bhuvnesh Jain
Editorialist: Bhuvnesh Jain
Difficulty
CAKEWALK
Prerequisites
Looping Techniques, Sorting Algorithms
Problem
Find the number of students remaining in the class if their name is recorded everytime they enter or leave the room.
Explanation
It is easy to see that at the end of the class only those students remain in the class who name was written an odd number of times.
To check the above condition, we can do a brute force by finding the number of times each name occurs and if it occurs an odd number of times, update the answer by 1. To compare 2 names, we can simply iterate over them character by character and also check it their lengths are same or not.
One other efficient way is to sort all the student names. Then, same names occur as a subarray, so counting the frequency will be linear in this case as compared to quadratic in the previous case.
Time Complexity
O(X * \log{X}), per test case, where |X| = Sum of lengths of all strings.
or O(N * N * |S|), per test case, where |S| = Maximum length of any string in the test case.
Space Complexity
O(X), where |X| = Sum of lengths of all strings.