Smallest Common Region
Real-world version: Innermost Zone Holding Both SitesThe statement here is our own: the same inputs, outputs and technique.
A survey is organised into zones. Each entry of regions names a zone first and then the zones lying directly inside it. A zone lies inside another when it is named directly inside it, or inside something that does.
No zone lies directly inside more than one zone, and one zone holds all the others.
Given two distinct zone names region1 and region2, return the name of the innermost zone that holds both of them. A zone counts as holding itself.
Examples
Example 1
- Input
- regions = [["World", "North", "South"], ["North", "Alba", "Cymru"], ["South", "Kernow"]], region1 = "Alba", region2 = "Cymru"
- Output
- "North"
Both sites lie directly inside "North", so that is the innermost zone holding them both.
Example 2
- Input
- regions = [["World", "North", "South"], ["North", "Alba", "Cymru"], ["South", "Kernow"]], region1 = "Alba", region2 = "Kernow"
- Output
- "World"
One site lies inside "North" and the other inside "South", and the innermost zone holding both of those is "World".
Example 3
- Input
- regions = [["World", "North", "South"], ["North", "Alba", "Cymru"], ["South", "Kernow"]], region1 = "Alba", region2 = "North"
- Output
- "North"
A zone counts as holding itself, and "Alba" lies inside "North", so "North" holds them both.
Constraints
- 2 <= regions.length <= 10^4
- 2 <= regions[i].length <= 20
- 1 <= regions[i][j].length <= 20
- 1 <= region1.length <= 20
- 1 <= region2.length <= 20
- region1 and region2 are different names
- Every name consists of English letters only
- No zone lies directly inside more than one zone, and one zone holds all the others
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def innermost_holder(regions: list[list[str]], region1: str, region2: str) -> str:public String innermostHolder(List<List<String>> regions, String region1, String region2)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Accounts MergeReal-world version: Merge Crew Badge RecordsMedium
- Step-By-Step Directions From a Binary Tree Node to AnotherReal-world version: Relay Route Between Two StationsMedium
- Smallest String With SwapsReal-world version: Smallest Label After Allowed ExchangesMedium
- Apply SubstitutionsReal-world version: Render A Config TemplateMedium