Destination City
LeetCode 1436 · View on LeetCode
Find the city that has no outgoing path — it never appears as a source. Collect all sources into a set, then find the destination not in that set.
package main
import "fmt"
func destCity(paths [][]string) string {
sources := map[string]bool{}
for _, p := range paths {
sources[p[0]] = true
}
for _, p := range paths {
if !sources[p[1]] {
return p[1]
}
}
return ""
}
func main() {
paths := [][]string{{"London", "New York"}, {"New York", "Lima"}, {"Lima", "Sao Paulo"}}
fmt.Println(destCity(paths)) // Sao Paulo
}