Trains the technique from
LeetCode 2246Longest Path With Different Adjacent CharactersThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tree has posts numbered 0 through n - 1, and post 0 sits at the top. above[i] names the post directly above post i, and above[0] is -1. Each post carries one lowercase letter, given by marks.
A run is a path through the tree that never repeats a post and along which no two neighbouring posts carry the same letter.
Return the largest number of posts a run can hold.
Example 1
The posts form a single chain and no two neighbours share a letter, so the whole chain is one run.
Example 2
Both posts carry the same letter, so no run can hold the two of them.
Example 3
Five posts hang off the top one, all carrying different letters, so a run can come up one branch, through the top, and down another.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_path(above: list[int], marks: str) -> int:public int longestPath(int[] above, String marks)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.