← 返回 pinterest 的题目列表Implement an In-Memory Table Join
类型:online_judge
Problem: Implement an In-Memory Table Join
You are given two tables represented as list[list[str]]:
Row 0 of each table is the header.
The remaining rows are data records.
All cell values can be treated as strings.
You are also given a column name key, which exists in both tables. Implement an inner join on the two tables using key, and return the joined table.
Output Rules
The output header consists of:
all columns from the first table;
followed by all columns from the second table except the duplicate key column.
For each row in the first table, in its original order, find all rows in the second table with the same key value.
If matching rows exist, output one joined row for each match.
If no matching row exists, skip that row.
If the second table contains duplicate keys, output all matching combinations while preserving their relative order in the second table.
Input Format
For online judging, use the following stdin format:
n c1
<table1 header>
<table1 row 1>
...
<table1 row n>
m c2
<table2 header>
<table2 row 1>
...
<table2 row m>
key
Where:
n is the number of data rows in the first table, excluding the header.
c1 is the number of columns in the first table.
m is the number of data rows in the second table, excluding the header.
c2 is the number of columns in the second table.
Table rows are comma-separated.
Cell values do not contain commas.
Output Format
Print the joined table. Each row should be comma-separated. The first row must be the output header.
Constraints
0 <= n, m <= 200000
1 <= c1, c2 <= 50
key exists in both table headers.
Every input row has a valid number of columns.
Example
Input:
2 2
id,name
1,Alice
2,Bob
2 2
id,city
1,SF
2,NY
id
Output:
id,name,city
1,Alice,SF
2,Bob,NY
Example
Input
2 2
id,name
1,Alice
2,Bob
2 2
id,city
1,SF
2,NY
id
Output
id,name,city
1,Alice,SF
2,Bob,NY