Class Solution
java.lang.Object
g2201_2300.s2246_longest_path_with_different_adjacent_characters.Solution
2246 - Longest Path With Different Adjacent Characters.<p>Hard</p>
<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><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/03/25/testingdrawio.png" alt="" /></p>
<p><strong>Input:</strong> parent = [-1,0,0,1,1,2], s = “abacbe”</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned.</p>
<p>It can be proven that there is no longer path that satisfies the conditions.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/03/25/graph2drawio.png" alt="" /></p>
<p><strong>Input:</strong> parent = [-1,0,0,0], s = “aabc”</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == parent.length == s.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>0 <= parent[i] <= n - 1</code> for all <code>i >= 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>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
longestPath
-