Obsidian

Search

Search IconIcon to open search

2246. Longest Path With Different Adjacent Characters

Last updated Jan 17, 2023

# Problem

Leetcode

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

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

Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.

Example 1:

Input: parent = [-1,0,0,1,1,2], s = “abacbe” Output: 3 Explanation: 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. It can be proven that there is no longer path that satisfies the conditions.

Example 2:

Input: parent = [-1,0,0,0], s = “aabc” Output: 3 Explanation: 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.

# Insight

After reading the problem, the first idea in my mind is DFS, since that’s the common way to solve Tree problems.

Given a node, we can determine the longest path that starts at from it by its children. There are some cases we might consider:

# Without any children

This is the simplest case, and the longest path is 1

# At least one child

# Submission

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package leetcode.daily  
  
class LongestPathWithDifferentAdjacentCharacters {  
    var ans = 1  
  
    class Node(val label: Char) {  
        val children: MutableList<Node> = mutableListOf();  
    }  
  
    fun longestPath(parent: IntArray, s: String): Int {  
        val chars = s.toCharArray()  
        val nodes = Array(s.length) { Node('a') }  
        for (i in s.indices) {  
            nodes[i] = Node(chars[i])  
        }  
        for (i in 1 until parent.size) {  
            nodes[parent[i]].children.add(nodes[i])  
        }  
  
        dfs(nodes[0], nodes)  
  
        return ans  
    }  
  
    private fun dfs(root: Node, nodes: Array<Node>): Int {  
        if (root.children.isEmpty()) return 1  
        val l = IntArray(root.children.size) { 0 }  
        for (i in root.children.indices) {  
            if (root.children[i].label == root.label) {  
                l[i] = 0  
                dfs(root.children[i], nodes)  
            } else {  
                l[i] = dfs(root.children[i], nodes)  
            }  
        }  
        l.sortDescending()  
        val result = if (l.size == 1) {  
            1 + l[0]  
        } else {  
            l[0] + 1 + l[1]  
        }  
  
        ans = ans.coerceAtLeast(result)  
        return l[0] + 1  
    }  
}