810. Reconstruct Itinerary — Euler Path Exists from JFK

HardRecursionRecursion

Given a list of airline tickets as [from, to] pairs, determine if an Eulerian path starting from 'JFK' exists that uses every ticket exactly once. Return true/false. Input: JSON {tickets: [[from,to],...]}.

Input: JSON {tickets}.

Output: Boolean.

Examples

Example 1
Input: {"tickets":[["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]}
Output: true
Explanation: An Euler path from JFK exists.
Example 2
Input: {"tickets":[["JFK","A"],["JFK","B"]]}
Output: false
Explanation: JFK has out-degree 2 but no return edges; no Euler path uses both.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →