Method
findItinerary
(self, tickets: List[List[str]])
Source from the content-addressed store, hash-verified
| 1 | class Solution: |
| 2 | def findItinerary(self, tickets: List[List[str]]) -> List[str]: |
| 3 | adj = {src: [] for src, dst in tickets} |
| 4 | res = [] |
| 5 | |
| 6 | for src, dst in tickets: |
| 7 | adj[src].append(dst) |
| 8 | |
| 9 | for key in adj: |
| 10 | adj[key].sort() |
| 11 | |
| 12 | def dfs(adj, src): |
| 13 | if src in adj: |
| 14 | destinations = adj[src][:] |
| 15 | while destinations: |
| 16 | dest = destinations[0] |
| 17 | adj[src].pop(0) |
| 18 | dfs(adj, dest) |
| 19 | destinations = adj[src][:] |
| 20 | res.append(src) |
| 21 | |
| 22 | dfs(adj, "JFK") |
| 23 | res.reverse() |
| 24 | |
| 25 | if len(res) != len(tickets) + 1: |
| 26 | return [] |
| 27 | |
| 28 | return res |
Callers
nothing calls this directly
Tested by
no test coverage detected