Can All Files Compile?
Problem statement
You are given unique file identifiers in files. The parallel array dependencies describes the files that each file needs: dependencies[i] lists every direct dependency of files[i].
A file can compile after all its dependencies have compiled. Determine whether all supplied files can compile. A missing dependency or a circular dependency makes this impossible.
For this exercise, assume identifiers are case-sensitive and only files named in files are available. Repeating a dependency name adds no extra requirement. Return true when every file can compile and false otherwise. An empty file list returns true.
Function
canCompileFiles(files: String[], dependencies: String[][]) → booleanExamples
Example 1
files = ["main","util","base"]dependencies = [["util"],["base"],[]]return = trueCompile base first, then util, then main.
Example 2
files = ["a","b"]dependencies = [["b"],["a"]]return = falseEach file waits for the other, so neither can compile first.
Example 3
files = ["app"]dependencies = [["lib"]]return = falselib is not among the available files.
Constraints
0 <= files.length <= 200, anddependencies.length = files.length.- File identifiers are distinct.
- Every identifier contains
1to24English letters, digits, underscores, hyphens or periods. - The total number of dependency entries is at most
2000. - Dependency identifiers may be absent from
files, repeated within a row, or equal to the file itself.