Class Solution

java.lang.Object
g1401_1500.s1436_destination_city.Solution

public class Solution extends Object
1436 - Destination City.<p>Easy</p> <p>You are given the array <code>paths</code>, where <code>paths[i] = [cityA<sub>i</sub>, cityB<sub>i</sub>]</code> means there exists a direct path going from <code>cityA<sub>i</sub></code> to <code>cityB<sub>i</sub></code>. <em>Return the destination city, that is, the city without any path outgoing to another city.</em></p> <p>It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> paths = [[&ldquo;London&rdquo;,&ldquo;New York&rdquo;],[&ldquo;New York&rdquo;,&ldquo;Lima&rdquo;],[&ldquo;Lima&rdquo;,&ldquo;Sao Paulo&rdquo;]]</p> <p><strong>Output:</strong> &ldquo;Sao Paulo&rdquo;</p> <p><strong>Explanation:</strong> Starting at &ldquo;London&rdquo; city you will reach &ldquo;Sao Paulo&rdquo; city which is the destination city. Your trip consist of: &ldquo;London&rdquo; -> &ldquo;New York&rdquo; -> &ldquo;Lima&rdquo; -> &ldquo;Sao Paulo&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> paths = [[&ldquo;B&rdquo;,&ldquo;C&rdquo;],[&ldquo;D&rdquo;,&ldquo;B&rdquo;],[&ldquo;C&rdquo;,&ldquo;A&rdquo;]]</p> <p><strong>Output:</strong> &ldquo;A&rdquo;</p> <p><strong>Explanation:</strong> All possible trips are:</p> <p>&ldquo;D&rdquo; -> &ldquo;B&rdquo; -> &ldquo;C&rdquo; -> &ldquo;A&rdquo;.</p> <p>&ldquo;B&rdquo; -> &ldquo;C&rdquo; -> &ldquo;A&rdquo;.</p> <p>&ldquo;C&rdquo; -> &ldquo;A&rdquo;.</p> <p>&ldquo;A&rdquo;. Clearly the destination city is &ldquo;A&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> paths = [[&ldquo;A&rdquo;,&ldquo;Z&rdquo;]]</p> <p><strong>Output:</strong> &ldquo;Z&rdquo;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= paths.length <= 100</code></li> <li><code>paths[i].length == 2</code></li> <li><code>1 <= cityA<sub>i</sub>.length, cityB<sub>i</sub>.length <= 10</code></li> <li><code>cityA<sub>i</sub> != cityB<sub>i</sub></code></li> <li>All strings consist of lowercase and uppercase English letters and the space character.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details