Tap here to switch tabs
Problem
Submissions

Problem: Smallest Common Region

med
30 min
Try to solve the Smallest Common Region problem.

Statement

You are given a list of lists of strings regions, where each list regions[i] defines a containment relationship such that the first region regions[i][0] directly contains all the subsequent regions regions[i][j] for j≥1j \geq 1.

If region AA contains region BB and region BB contains region CC, then region AA also indirectly contains region CC.

Given two specific regions region1 and region2, return the smallest region that contains both region1 and region2.

Constraints:

  • 2≤2 \leq regions.length ≤104\leq 10^4

  • 2≤2 \leq regions[i].length ≤20\leq 20

  • 1≤1 \leq regions[i][j].length, region1.length, region2.length ≤20\leq 20

  • region1 ≠\neq region2

  • regions[i][j], region1, and region2 consist of English letters.

  • The input is generated such that there exists a region which contains all the other regions, either directly or indirectly.

  • A region cannot be directly contained in more than one region.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Smallest Common Region

med
30 min
Try to solve the Smallest Common Region problem.

Statement

You are given a list of lists of strings regions, where each list regions[i] defines a containment relationship such that the first region regions[i][0] directly contains all the subsequent regions regions[i][j] for j≥1j \geq 1.

If region AA contains region BB and region BB contains region CC, then region AA also indirectly contains region CC.

Given two specific regions region1 and region2, return the smallest region that contains both region1 and region2.

Constraints:

  • 2≤2 \leq regions.length ≤104\leq 10^4

  • 2≤2 \leq regions[i].length ≤20\leq 20

  • 1≤1 \leq regions[i][j].length, region1.length, region2.length ≤20\leq 20

  • region1 ≠\neq region2

  • regions[i][j], region1, and region2 consist of English letters.

  • The input is generated such that there exists a region which contains all the other regions, either directly or indirectly.

  • A region cannot be directly contained in more than one region.