Grid shortest path with portals
Your task
A grid contains 'S' start, 'T' target, '.' floor, '#' wall, and matched portal pairs marked by lowercase letters (each letter appears on at most two cells). A normal step to an orthogonally adjacent non-wall cell costs 1. Standing on a portal cell, you may teleport instantly (cost 0) to the other cell with the same letter. Return the minimum cost to get from 'S' to 'T', or -1 if unreachable. Exactly one 'S' and one 'T' exist.
Examples
min_steps_portals(grid: list[list[str]]) -> intExample 1
Input
min_steps_portals([["S", ".", "a", "#", "#", "#", "a", ".", "T"]])Output
4