<p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size <code>n</code>, where <code>parent[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parent[0] == -1</code>.</p>

<p>You are also given a string <code>s</code> of length <code>n</code>, where <code>s[i]</code> is the character assigned to node <code>i</code>.</p>

<p>Return <em>the length of the <strong>longest path</strong> in the tree such that no pair of <strong>adjacent</strong> nodes on the path have the same character assigned to them.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/testingdrawio.png" style="width: 201px; height: 241px;" />
<pre>
<strong>Input:</strong> parent = [-1,0,0,1,1,2], s = &quot;abacbe&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -&gt; 1 -&gt; 3. The length of this path is 3, so 3 is returned.
It can be proven that there is no longer path that satisfies the conditions. 
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/graph2drawio.png" style="width: 201px; height: 221px;" />
<pre>
<strong>Input:</strong> parent = [-1,0,0,0], s = &quot;aabc&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters is the path: 2 -&gt; 0 -&gt; 3. The length of this path is 3, so 3 is returned.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == parent.length == s.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= parent[i] &lt;= n - 1</code> for all <code>i &gt;= 1</code></li>
	<li><code>parent[0] == -1</code></li>
	<li><code>parent</code> represents a valid tree.</li>
	<li><code>s</code> consists of only lowercase English letters.</li>
</ul>

class Solution:
    # not super hard , main gotcha is to keep track of longest and secondlongest since
    # we are not limited to binary tree , there could be more than 2 childrent here
    # Time - O(n) , space - O(N) ,  we do not need visited here coz the question states there are no cycles
    def __init__(self):
        self.ans = 1

    def dfs(self,curr,s,graph):

        # meaning its a leaf node
        if curr not in graph:
            return 1

        longest = secondlongest = 0
        for nei in graph[curr]:
            
            depthFromcurr = self.dfs(nei,s,graph)
            # very tricky edge case is graph like >  a > a > b
            # this asks us to explore the graph even if the child(a) is same as parent(a)
            # why ? because that child(a) has a diff char(b)
            # but the depth received from them is useless for its own calculation
            # as well as to bubble up above , hence we do not update longest and secondlongest
            # but a call to dfs ensures that the node is not skipped to see if it has a longest path starting from itself and self contained in itself
            if s[curr]!=s[nei]:
                if depthFromcurr > longest:
                    secondlongest = longest
                    longest = depthFromcurr
                elif depthFromcurr > secondlongest:
                    secondlongest = depthFromcurr

        self.ans = max(self.ans,1+longest+secondlongest)

        return 1+longest        


        return path_len

    def longestPath(self, parent: List[int], s: str) -> int:
        graph = defaultdict(list)
        for child,parent in enumerate(parent):
            graph[parent].append(child)
        
        self.dfs(0,s,graph) 
        return self.ans
        
994