Class Solution

java.lang.Object
g1401_1500.s1436_destination_city.Solution

public class Solution extends Object
1436 - Destination City.

Easy

You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.

It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.

Example 1:

Input: paths = [[β€œLondon”,β€œNew York”],[β€œNew York”,β€œLima”],[β€œLima”,β€œSao Paulo”]]

Output: β€œSao Paulo”

Explanation: Starting at β€œLondon” city you will reach β€œSao Paulo” city which is the destination city. Your trip consist of: β€œLondon” -> β€œNew York” -> β€œLima” -> β€œSao Paulo”.

Example 2:

Input: paths = [[β€œB”,β€œC”],[β€œD”,β€œB”],[β€œC”,β€œA”]]

Output: β€œA”

Explanation: All possible trips are:

β€œD” -> β€œB” -> β€œC” -> β€œA”.

β€œB” -> β€œC” -> β€œA”.

β€œC” -> β€œA”.

β€œA”. Clearly the destination city is β€œA”.

Example 3:

Input: paths = [[β€œA”,β€œZ”]]

Output: β€œZ”

Constraints:

  • 1 <= paths.length <= 100
  • paths[i].length == 2
  • 1 <= cityAi.length, cityBi.length <= 10
  • cityAi != cityBi
  • All strings consist of lowercase and uppercase English letters and the space character.
  • Constructor Details

    • Solution

      public Solution()
  • Method Details