Shortest path through a maze with free portals

Write codeHard35 min

Your task

You're navigating a grid: 'S' is the start, 'E' the exit, '.' is walkable, '#' is a wall. Each lowercase letter marks a portal; portals that share the same letter are linked, and standing on one lets you teleport to its partner tile at no movement cost (exactly two tiles per letter). Walking to an orthogonally adjacent non-wall tile costs 1 step. Return the minimum number of steps to get from S to E, or -1 if unreachable.

Examples

portal_maze(grid: list[str]) -> int
Example 1
Inputportal_maze(["S.a..", "###.#", "E.a.."])
Output4