Student and Course Enrollment
Problem statement
Build a small enrollment system with students and courses. You get the calls as a list of operation strings, in order:
"enroll student course"adds the student to the course. If the student is already in the course, nothing changes."disenroll student course"removes the student from the course. If the student is not in the course, nothing changes."get_students course"returns the names of the students now in the course, in the order they enrolled."get_courses student"returns the names of the courses the student is now in, in the order the student enrolled in them.
A student who disenrolls and later enrolls again counts as a new enrollment, so the student and the course each move to the end of the other's list.
A name with no current enrollments returns an empty list. Student names and course names are separate, so a student and a course can have the same name without affecting each other.
Return one row for each get_students or get_courses operation, in the order those operations appear. You can use any classes you like, such as Student and Course.
Function
runEnrollment(operations: String[]) → String[][]Examples
Example 1
operations = ["enroll ana math","enroll ben math","enroll ana art","get_students math","get_courses ana","disenroll ana math","get_students math","get_courses ana"]return = [["ana","ben"],["math","art"],["ben"],["art"]]ana and ben join math, and ana also joins art.
get_studentsformathreturnsanathenben, the order they enrolled.get_coursesforanareturnsmaththenart.- After
analeavesmath, the course has onlyben, andanahas onlyart.
Example 2
operations = ["enroll ana math","enroll ben math","enroll ana math","get_students math","disenroll cara math","disenroll ana art","get_students math","disenroll ana math","enroll ana math","get_students math"]return = [["ana","ben"],["ana","ben"],["ben","ana"]]- The second
enrollofanainmathchanges nothing, so the first result isanathenben. carais not inmathandanais not inart, so neitherdisenrollchanges anything. The second result is the same as the first.anathen leaves and enrolls again, which movesanato the end. The third result isbenthenana.
Example 3
operations = ["get_students math","enroll math art","get_students math","get_courses math","get_students art","disenroll math art","get_courses math"]return = [[],[],["art"],["math"],[]]- No one has enrolled yet, so the first
get_studentsreturns an empty row. - Next, a student named
mathjoins the courseart. There is still no course namedmathwith students, soget_studentsformathis empty. get_coursesfor the studentmathreturnsart, andget_studentsforartreturnsmath.- After the student leaves,
get_coursesformathis empty again.
Constraints
1 <= operations.length <= 1000- Every operation is exactly
"enroll student course","disenroll student course","get_students course", or"get_courses student", with single spaces between the parts. - Every student and course name has
1to10characters, and each character is a lowercase English letter or a digit. - At least one operation is
get_studentsorget_courses. - All returned rows together hold at most
2000names.