Loading problem...
You are given a directed graph consisting of n nodes labeled from 0 to n - 1, where each node is assigned a color represented by a lowercase English letter.
The graph structure is defined by two inputs:
colors of length n, where colors[i] represents the color of the i-th node (0-indexed).edges where each edges[j] = [sourceⱼ, destinationⱼ] represents a directed edge from node sourceⱼ to node destinationⱼ.A valid path in this graph is any sequence of nodes v₁ → v₂ → v₃ → ... → vₖ where there exists a directed edge from vᵢ to vᵢ₊₁ for every 1 ≤ i < k. A path can consist of a single node (length 1).
The color frequency value of a path is defined as the maximum count of any single color appearing among the nodes along that path. In other words, it is the frequency of the most frequently occurring color across all nodes in that path.
Your task is to determine the largest color frequency value among all valid paths in the graph. If the graph contains a cycle (making it impossible to define finite non-repeating paths), return -1.
colors = "abaca"
edges = [[0,1],[0,2],[2,3],[3,4]]3Consider the path 0 → 2 → 3 → 4. The colors along this path are 'a', 'a', 'c', 'a' respectively. The color 'a' appears 3 times, which is the maximum frequency for any single color along this path. No other valid path in this graph yields a higher color frequency value.
colors = "a"
edges = [[0,0]]-1The edge [0,0] creates a self-loop, which constitutes a cycle. Since the graph contains a cycle, we return -1.
colors = "a"
edges = []1The graph contains a single node with color 'a' and no edges. The only valid path is the node itself, containing exactly one occurrence of color 'a'. Therefore, the maximum color frequency value is 1.
Constraints