Skip to main content
Back to problems
#1623
Easy Database

All valid triplets that can represent a country

Database
80.9% acceptance
Mar 31, 2026
76
144

No description available.

Solution

Pandas
Time O(1)
Space O(1)
LeetCode
solution.pandas
# Table: SchoolA
# 
# +---------------+---------+
# | Column Name   | Type    |
# +---------------+---------+
# | student_id    | int     |
# | student_name  | varchar |
# +---------------+---------+
# student_id is the column with unique values for this table.
# Each row of this table contains the name and the id of a student in school A.
# All student_name are distinct.
# 
#  
# 
# Table: SchoolB
# 
# +---------------+---------+
# | Column Name   | Type    |
# +---------------+---------+
# | student_id    | int     |
# | student_name  | varchar |
# +---------------+---------+
# student_id is the column with unique values for this table.
# Each row of this table contains the name and the id of a student in school B.
# All student_name are distinct.
# 
#  
# 
# Table: SchoolC
# 
# +---------------+---------+
# | Column Name   | Type    |
# +---------------+---------+
# | student_id    | int     |
# | student_name  | varchar |
# +---------------+---------+
# student_id is the column with unique values for this table.
# Each row of this table contains the name and the id of a student in school C.
# All student_name are distinct.
# 
#  
# 
# There is a country with three schools, where each student is enrolled in exactly one school. The country is joining a competition and wants to select one student from each school to represent the country such that:
# 
# member_A is selected from SchoolA,
# 
# member_B is selected from SchoolB,
# 
# member_C is selected from SchoolC, and
# 
# The selected students' names and IDs are pairwise distinct (i.e. no two students share the same name, and no two students share the same ID).
# 
# Write a solution to find all the possible triplets representing the country under the given constraints.
# 
# Return the result table in any order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# SchoolA table:
# +------------+--------------+
# | student_id | student_name |
# +------------+--------------+
# | 1          | Alice        |
# | 2          | Bob          |
# +------------+--------------+
# SchoolB table:
# +------------+--------------+
# | student_id | student_name |
# +------------+--------------+
# | 3          | Tom          |
# +------------+--------------+
# SchoolC table:
# +------------+--------------+
# | student_id | student_name |
# +------------+--------------+
# | 3          | Tom          |
# | 2          | Jerry        |
# | 10         | Alice        |
# +------------+--------------+
# Output:
# +----------+----------+----------+
# | member_A | member_B | member_C |
# +----------+----------+----------+
# | Alice    | Tom      | Jerry    |
# | Bob      | Tom      | Alice    |
# +----------+----------+----------+
# Explanation:
# Let us see all the possible triplets.
# - (Alice, Tom, Tom) --> Rejected because member_B and member_C have the same name and the same ID.
# - (Alice, Tom, Jerry) --> Valid triplet.
# - (Alice, Tom, Alice) --> Rejected because member_A and member_C have the same name.
# - (Bob, Tom, Tom) --> Rejected because member_B and member_C have the same name and the same ID.
# - (Bob, Tom, Jerry) --> Rejected because member_A and member_C have the same ID.
# - (Bob, Tom, Alice) --> Valid triplet.

import pandas as pd

def find_valid_triplets(school_a: pd.DataFrame, school_b: pd.DataFrame, school_c: pd.DataFrame) -> pd.DataFrame:
  school_a = school_a.rename(columns={'student_id': 'id_a', 'student_name': 'member_A'})
  school_b = school_b.rename(columns={'student_id': 'id_b', 'student_name': 'member_B'})
  school_c = school_c.rename(columns={'student_id': 'id_c', 'student_name': 'member_C'})
  cross = school_a.merge(school_b, how='cross').merge(school_c, how='cross')
  cross = cross[
    (cross['member_A'] != cross['member_B']) &
    (cross['member_A'] != cross['member_C']) &
    (cross['member_B'] != cross['member_C']) &
    (cross['id_a'] != cross['id_b']) &
    (cross['id_a'] != cross['id_c']) &
    (cross['id_b'] != cross['id_c'])
  ]
  return cross[['member_A', 'member_B', 'member_C']]