Longest Name Chain in a Directed Acyclic Graph
Problem statement
Each row [from, to] in edges is a directed connection between two names. The complete graph is guaranteed to be acyclic.
A name chain is a directed path. Its length is the number of names on that path, including both endpoints. Return the maximum chain length. Duplicate rows describe the same directed edge and must not increase the result. Return 0 when edges is empty.
Function
longestNameChain(edges: String[][]) → intExamples
Example 1
edges = [["a","b"],["b","c"],["d","e"]]return = 3The longest chain is a -> b -> c, which contains three names.
Example 2
edges = [["amy","bo"],["amy","cy"],["bo","dee"],["cy","dee"],["dee","eve"]]return = 4Either branch from amy reaches eve through four names.
Example 3
edges = []return = 0No names are present.
Constraints
0 <= edges.length <= 100000.- Every row contains exactly two nonempty names.
- Names contain 1 to 40 visible ASCII characters.
- The graph described by the distinct edges is a directed acyclic graph.