You are given some lists of regions where the first region of each list directly contains all other regions in that list.

If a region x contains a region y directly, and region y contains region z directly, then region x is said to contain region z indirectly.

Note that region x also indirectly contains all regions indirectly contained in y. Naturally, if a region x contains (either directly or indirectly) another region y, then x is bigger than or equal to y in size.

Also, by definition, a region x contains itself.

Given two regions: region1 and region2, return the smallest region that contains both of them.

It is guaranteed the smallest region exists.

Input & Output

Example 1 — Basic Hierarchy
$ Input: regions = [["Earth","North America","South America"],["North America","United States","Canada"],["United States","New York","Boston"],["Canada","Ontario","Quebec"],["South America","Brazil"]], region1 = "Quebec", region2 = "New York"
Output: "North America"
💡 Note: Quebec → Canada → North America → Earth, New York → United States → North America → Earth. First common ancestor is North America.
Example 2 — Same Region
$ Input: regions = [["Earth","North America","South America"],["North America","United States","Canada"]], region1 = "Canada", region2 = "Canada"
Output: "Canada"
💡 Note: Both regions are the same, so Canada contains itself.
Example 3 — Parent-Child Relationship
$ Input: regions = [["Earth","North America"],["North America","United States"]], region1 = "United States", region2 = "North America"
Output: "North America"
💡 Note: North America directly contains United States, so North America is the smallest common region.

Constraints

  • 1 ≤ regions.length ≤ 104
  • 1 ≤ regions[i].length ≤ 20
  • 1 ≤ regions[i][j].length ≤ 20
  • All strings consist of English letters and spaces with at most 20 characters.
  • All strings are unique.
  • It is guaranteed a valid answer exists.

Visualization

Tap to expand
Smallest Common Region INPUT Region Hierarchy Tree Earth North America South America United States Canada Brazil New York Boston Ontario Quebec Input Parameters region1 = "Quebec" region2 = "New York" Find smallest region containing both highlighted regions ALGORITHM STEPS 1 Build Parent Map Map each region to parent Quebec --> Canada New York --> United States Canada --> North America ... 2 Get Path for Quebec Traverse to root, store path Quebec-->Canada-->N.America-->Earth 3 Store in Set Add Quebec's path to HashSet Set: {Quebec,Canada,N.America,Earth} 4 Find First Match Traverse New York's path New York in Set? NO United States in Set? NO North America in Set? OK! First common ancestor found FINAL RESULT Common Ancestor Found North America United States Canada New York Quebec OUTPUT "North America" Verification - Quebec is under N.America - New York is under N.America - Smallest such region: OK Key Insight: Direct Path Comparison uses O(h) space where h is tree height. Build path from region1 to root, store in HashSet. Then traverse from region2 upward - first node found in Set is the LCA. Time: O(n) to build parent map + O(h) for path finding. This is similar to LCA in binary trees. TutorialsPoint - Smallest Common Region | Direct Path Comparison Approach
Asked in
Amazon 15 Google 8 Microsoft 5
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen