All the Matches of the League — LeetCode 2339 Python Solution

EasyLeetCode PremiumDatabase
Problem
#2339
Reading time
2 min

Table schema

SQL
Table: Teams +-------------+---------+ | Column Name | Type | +-------------+---------+ | team_name | varchar | +-------------+---------+ team_name is the column with unique values of this table. Each row of this table shows the name of a team.

Example

SQL
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| team_name   | varchar |
+-------------+---------+
team_name is the column with unique values of this table.
Each row of this table shows the name of a team.

Python solution

Python
import duckdb
import pandas as pd

def solution(teams: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Teams", teams)
    return con.execute("""SELECT t1.team_name AS home_team, t2.team_name AS away_team
FROM
    Teams AS t1
    JOIN Teams AS t2
WHERE t1.team_name != t2.team_name;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2339. All the Matches of the League?
LeetCode 2339. All the Matches of the League is rated Easy on LeetCode.
What topics does LeetCode 2339. All the Matches of the League cover?
LeetCode 2339. All the Matches of the League is tagged Database on LeetCode.
Is LeetCode 2339. All the Matches of the League a premium problem?
Yes. LeetCode 2339. All the Matches of the League is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview