You are asked to simulate a simple hierarchical file system and implement an access-check function.
The file system consists of folders arranged in a tree. A user is granted direct access to a subset of folders. Access is inherited down the tree: if the user has access to a parent folder, they automatically have access to all of its descendant folders.
You are given:
/
├── A
│ ├── B
│ │ ├── C
│ │ └── D
│ └── E
└── F
could be represented as:
allFolders = [
"/",
"/A",
"/A/B",
"/A/B/C",
"/A/B/D",
"/A/E",
"/F"
]
accessibleFolders
representing folders to which the user has
direct
access. For example:
accessibleFolders = { "/A", "/F" }
You need to implement the function:
bool HasAccess(string folderPath)
that returns:
true
if the user has access to
folderPath
either
because:
folderPath
is in
accessibleFolders
,
or
folderPath
(e.g.,
/A
is an ancestor of
/A/B/C
) is in
accessibleFolders
.
false
otherwise.
Assume:
allFolders
and
accessibleFolders
are normalized absolute paths starting with
'/'
, with components separated by
'/'
(e.g.,
/A/B/C
).
folderPath
passed into
HasAccess
is always a valid folder path present in
allFolders
.
Examples (given the tree above and accessibleFolders = {"/A", "/F"}):
HasAccess("/A")
→
true
(direct access)
HasAccess("/A/B")
→
true
(inherits from
/A
)
HasAccess("/A/B/C")
→
true
(inherits from
/A
)
HasAccess("/A/E")
→
true
(inherits from
/A
)
HasAccess("/F")
→
true
(direct access)
HasAccess("/")
→
false
(no access to root unless
/
is in
accessibleFolders
)
Design and implement HasAccess so that it can be called many times efficiently after the initial inputs (allFolders and accessibleFolders) are known.