Class Solution

java.lang.Object
g0301_0400.s0332_reconstruct_itinerary.Solution

public class Solution extends Object
332 - Reconstruct Itinerary.<p>Hard</p> <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p> <p>All of the tickets belong to a man who departs from <code>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p> <ul> <li>For example, the itinerary <code>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</code>.</li> </ul> <p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" alt="" /></p> <p><strong>Input:</strong> tickets = [[&ldquo;MUC&rdquo;,&ldquo;LHR&rdquo;],[&ldquo;JFK&rdquo;,&ldquo;MUC&rdquo;],[&ldquo;SFO&rdquo;,&ldquo;SJC&rdquo;],[&ldquo;LHR&rdquo;,&ldquo;SFO&rdquo;]]</p> <p><strong>Output:</strong> [&ldquo;JFK&rdquo;,&ldquo;MUC&rdquo;,&ldquo;LHR&rdquo;,&ldquo;SFO&rdquo;,&ldquo;SJC&rdquo;]</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" alt="" /></p> <p><strong>Input:</strong> tickets = [[&ldquo;JFK&rdquo;,&ldquo;SFO&rdquo;],[&ldquo;JFK&rdquo;,&ldquo;ATL&rdquo;],[&ldquo;SFO&rdquo;,&ldquo;ATL&rdquo;],[&ldquo;ATL&rdquo;,&ldquo;JFK&rdquo;],[&ldquo;ATL&rdquo;,&ldquo;SFO&rdquo;]]</p> <p><strong>Output:</strong> [&ldquo;JFK&rdquo;,&ldquo;ATL&rdquo;,&ldquo;JFK&rdquo;,&ldquo;SFO&rdquo;,&ldquo;ATL&rdquo;,&ldquo;SFO&rdquo;]</p> <p><strong>Explanation:</strong></p> <pre><code> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= tickets.length <= 300</code></li> <li><code>tickets[i].length == 2</code></li> <li><code>from<sub>i</sub>.length == 3</code></li> <li><code>to<sub>i</sub>.length == 3</code></li> <li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li> <li><code>from<sub>i</sub> != to<sub>i</sub></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details