# LC5441 Bus Routes — BFS over Routes, Not Stops

**Approach.** Map stop→routes; BFS where each level is one more bus taken; mark routes visited so each route expands once.

```python
from collections import defaultdict, deque
def numBusesToDestination(routes, S, T):
    if S == T: return 0
    by = defaultdict(list)
    for i, r in enumerate(routes):
        for s in r: by[s].append(i)
    seen_r, seen_s, q = set(), {S}, deque([(S, 0)])
    while q:
        s, d = q.popleft()
        for ri in by[s]:
            if ri in seen_r: continue
            seen_r.add(ri)
            for n in routes[ri]:
                if n == T: return d + 1
                if n not in seen_s: seen_s.add(n); q.append((n, d + 1))
    return -1
```

**Interview note.** Say the invariant out loud before coding, then test an edge case. Practise these patterns with an [Linkjob AI](https://www.linkjob.ai/) — more at https://www.linkjob.ai/ .
