Given a list of tuples where there is two strings in each tuple (from, to) return True if a trip can be made using EXACTLY k flights else return False.
def PathWithKStops (boardingPasses, origin, dst, k):
Example Input #1:
boardingPasses = [ ('SEA','SLC'), ('SLC', 'SEA'), ('SEA', 'NYC'), ('NYC', 'SLC'), ('SEA', 'SLC') ]
origin = 'SEA'
dst = 'SEA'
k = 3Output:
returns True because you would go from SEA->NYC, NYC->SLC, SLC->SEA so exactly 3 flights.
Notice:
('SEA', 'SLC') so there's two ('SEA', 'SLC') passes)from will never be the same as the to (i.e. [ ('SEA', 'SEA') ] is NOT valid input)origin='SEA' , dst='SEA' is valid)Example Input #2 :
boardingPasses = [ ('SEA', 'SLC'), ('SLC', 'NYC'), ('SEA', 'NYC'), ('NYC', 'SEA') ]
src = 'NYC'
dst = 'SEA'
k = 2Output:
returns False because you can't go from NYC to SEA in exactly 2 flights
Would appreciate any help on how to approach this problem, I still can't figure out how to solve it.
UPDATE: I FINALLY SOLVED IT