Courses Shared by Every Student Pair
Problem statement
Each row of enrollments is [studentID,courseName]. Return the collection of courses shared by every pair of distinct students, including pairs with no common course.
For this exercise, assume only IDs appearing in the input are represented, duplicate enrollments have no effect, and IDs and course names are compared as complete case-sensitive strings. Different leading-zero ID spellings are distinct identifiers.
For this exercise, assume each unordered pair is returned once as [firstID,secondID,course1,...]. Put the two IDs in ASCII lexicographic order, sort the distinct common courses in that order, and sort result rows by their two IDs. A pair without a common course has exactly two fields.
For this exercise, assume empty input or one represented student returns an empty array.
Function
sharedCourses(enrollments: String[][]) → String[][]Examples
Example 1
enrollments = [["58","Linear Algebra"],["94","Art History"],["94","Operating Systems"],["17","Software Design"],["58","Mechanics"],["58","Economics"],["17","Linear Algebra"],["17","Political Science"],["94","Economics"],["25","Economics"],["58","Software Design"]]return = [["17","25"],["17","58","Linear Algebra","Software Design"],["17","94"],["25","58","Economics"],["25","94","Economics"],["58","94","Economics"]]Students 17 and 58 share Linear Algebra and Software Design. Every student pair appears, including the two pairs without a shared course.
Example 2
enrollments = [["0","Advanced Mechanics"],["0","Art History"],["1","Course 1"],["1","Course 2"],["2","Computer Architecture"],["3","Course 1"],["3","Course 2"],["4","Algorithms"]]return = [["0","1"],["0","2"],["0","3"],["0","4"],["1","2"],["1","3","Course 1","Course 2"],["1","4"],["2","3"],["2","4"],["3","4"]]Students 1 and 3 share Course 1 and Course 2; the other nine student pairs have no common course.
Constraints
- For this exercise, assume
0 <= enrollments.length <= 120and at most20distinct students. - For this exercise, assume at most
10distinct courses per student, completing the reported small-constant condition. - Each row has exactly two strings. For this exercise, assume IDs have
1through10decimal digits, and course names have1through30ASCII letters, digits or spaces.