Process Graph Rule Validation
Learn this problemProblem statement
A process system is represented by a directed acyclic graph. Node i has type processTypes.charAt(i), one of A, B, C, or D. Each row [u, v] in edges means that process u can be followed directly by process v.
A complete execution path starts at any node with indegree 0 and ends at any node with outdegree 0. The system is valid only if every complete execution path satisfies all three rules:
- The path contains at least one process of type
B. - No process of type
Aappears before a later process of typeBon the same path. - Every process of type
Dhas at least one earlier process of typeCon the same path.
Return true when every complete execution path is valid; otherwise return false.
Function
isValidProcessSystem(processTypes: String, edges: int[][]) → booleanExamples
Example 1
processTypes = "BCAD"edges = [[0,1],[1,2],[2,3]]return = trueThe only complete path is B -> C -> A -> D. It contains B, its B is before its A, and its D has an earlier C.
Example 2
processTypes = "AB"edges = [[0,1]]return = falseThe path contains an A before a later B, which violates the second rule.
Example 3
processTypes = "BCBD"edges = [[0,1],[0,2],[1,3],[2,3]]return = falseThe path 0 -> 2 -> 3 reaches D without an earlier C. One invalid complete path makes the whole system invalid.
Constraints
1 <= processTypes.length <= 100000- Every character of
processTypesisA,B,C, orD. 0 <= edges.length <= 200000- Every edge has the form
[u, v], where0 <= u, v < processTypes.lengthandu != v. - The edges are unique and the directed graph is acyclic.