Portal grid shortest path

Write codeHard35 minGoogle style

Your task

Given a grid of characters: 'S' is the start, 'E' is the exit, '#' is a wall, '.' is open floor, and any other letter denotes a teleport portal. You move up/down/left/right between non-wall cells, each step costing 1. Additionally, when you are standing on a portal cell you may, as a single step, teleport to any other cell sharing the same letter. Return the minimum number of steps from S to E, or -1 if unreachable. Assume each portal letter appears at most twice.

Examples

portal_shortest(grid: list[str]) -> int
Example 1
Inputportal_shortest(["S.A..", "#####", "..A.E"])
Output5