Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D character grid matrix of size m x n, represented as an array of strings, where matrix[i][j] represents the cell at the intersection of the ith row and jth column. Each cell is one of the following:
'.' representing an empty cell.'#' representing an obstacle.'A'-'Z') representing a teleportation portal.You start at the top-left cell (0, 0), and your goal is to reach the bottom-right cell (m - 1, n - 1). You can move from the current cell to any adjacent cell (up, down, left, right) as long as the destination cell is within the grid bounds and is not an obstacle.
If you step on a cell containing a portal letter and you haven't used that portal letter before, you may instantly teleport to any other cell in the grid with the same letter. This teleportation does not count as a move, but each portal letter can be used at most once during your journey.
Return the minimum number of moves required to reach the bottom-right cell. If it is not possible to reach the destination, return -1.
Example 1:
Input: matrix = ["A..",".A.","..."]
Output: 2
Explanation:
(0, 0) to (1, 1).(1, 1) to (1, 2).(1, 2) to (2, 2).Example 2:
Input: matrix = [".#...",".#.#.",".#.#.","...#."]
Output: 13
Explanation:
Constraints:
1 <= m == matrix.length <= 1031 <= n == matrix[i].length <= 103matrix[i][j] is either '#', '.', or an uppercase English letter.matrix[0][0] is not an obstacle.Problem summary: You are given a 2D character grid matrix of size m x n, represented as an array of strings, where matrix[i][j] represents the cell at the intersection of the ith row and jth column. Each cell is one of the following: '.' representing an empty cell. '#' representing an obstacle. An uppercase letter ('A'-'Z') representing a teleportation portal. You start at the top-left cell (0, 0), and your goal is to reach the bottom-right cell (m - 1, n - 1). You can move from the current cell to any adjacent cell (up, down, left, right) as long as the destination cell is within the grid bounds and is not an obstacle. If you step on a cell containing a portal letter and you haven't used that portal letter before, you may instantly teleport to any other cell in the grid with the same letter. This teleportation does not count as a move, but each portal letter can be used at most once during your
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["A..",".A.","..."]
[".#...",".#.#.",".#.#.","...#."]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3552: Grid Teleportation Traversal
class Solution {
public int minMoves(String[] matrix) {
int m = matrix.length, n = matrix[0].length();
Map<Character, List<int[]>> g = new HashMap<>();
for (int i = 0; i < m; i++) {
String row = matrix[i];
for (int j = 0; j < n; j++) {
char c = row.charAt(j);
if (Character.isAlphabetic(c)) {
g.computeIfAbsent(c, k -> new ArrayList<>()).add(new int[] {i, j});
}
}
}
int[] dirs = {-1, 0, 1, 0, -1};
int INF = Integer.MAX_VALUE / 2;
int[][] dist = new int[m][n];
for (int[] arr : dist) Arrays.fill(arr, INF);
dist[0][0] = 0;
Deque<int[]> q = new ArrayDeque<>();
q.add(new int[] {0, 0});
while (!q.isEmpty()) {
int[] cur = q.pollFirst();
int i = cur[0], j = cur[1];
int d = dist[i][j];
if (i == m - 1 && j == n - 1) return d;
char c = matrix[i].charAt(j);
if (g.containsKey(c)) {
for (int[] pos : g.get(c)) {
int x = pos[0], y = pos[1];
if (d < dist[x][y]) {
dist[x][y] = d;
q.addFirst(new int[] {x, y});
}
}
g.remove(c);
}
for (int idx = 0; idx < 4; idx++) {
int a = dirs[idx], b = dirs[idx + 1];
int x = i + a, y = j + b;
if (0 <= x && x < m && 0 <= y && y < n && matrix[x].charAt(y) != '#'
&& d + 1 < dist[x][y]) {
dist[x][y] = d + 1;
q.addLast(new int[] {x, y});
}
}
}
return -1;
}
}
// Accepted solution for LeetCode #3552: Grid Teleportation Traversal
type pair struct{ x, y int }
func minMoves(matrix []string) int {
m, n := len(matrix), len(matrix[0])
g := make(map[rune][]pair)
for i := 0; i < m; i++ {
for j, c := range matrix[i] {
if unicode.IsLetter(c) {
g[c] = append(g[c], pair{i, j})
}
}
}
dirs := []int{-1, 0, 1, 0, -1}
INF := 1 << 30
dist := make([][]int, m)
for i := range dist {
dist[i] = make([]int, n)
for j := range dist[i] {
dist[i][j] = INF
}
}
dist[0][0] = 0
q := list.New()
q.PushBack(pair{0, 0})
for q.Len() > 0 {
cur := q.Remove(q.Front()).(pair)
i, j := cur.x, cur.y
d := dist[i][j]
if i == m-1 && j == n-1 {
return d
}
c := rune(matrix[i][j])
if v, ok := g[c]; ok {
for _, p := range v {
x, y := p.x, p.y
if d < dist[x][y] {
dist[x][y] = d
q.PushFront(pair{x, y})
}
}
delete(g, c)
}
for idx := 0; idx < 4; idx++ {
x, y := i+dirs[idx], j+dirs[idx+1]
if 0 <= x && x < m && 0 <= y && y < n && matrix[x][y] != '#' && d+1 < dist[x][y] {
dist[x][y] = d + 1
q.PushBack(pair{x, y})
}
}
}
return -1
}
# Accepted solution for LeetCode #3552: Grid Teleportation Traversal
class Solution:
def minMoves(self, matrix: List[str]) -> int:
m, n = len(matrix), len(matrix[0])
g = defaultdict(list)
for i, row in enumerate(matrix):
for j, c in enumerate(row):
if c.isalpha():
g[c].append((i, j))
dirs = (-1, 0, 1, 0, -1)
dist = [[inf] * n for _ in range(m)]
dist[0][0] = 0
q = deque([(0, 0)])
while q:
i, j = q.popleft()
d = dist[i][j]
if i == m - 1 and j == n - 1:
return d
c = matrix[i][j]
if c in g:
for x, y in g[c]:
if d < dist[x][y]:
dist[x][y] = d
q.appendleft((x, y))
del g[c]
for a, b in pairwise(dirs):
x, y = i + a, j + b
if (
0 <= x < m
and 0 <= y < n
and matrix[x][y] != "#"
and d + 1 < dist[x][y]
):
dist[x][y] = d + 1
q.append((x, y))
return -1
// Accepted solution for LeetCode #3552: Grid Teleportation Traversal
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3552: Grid Teleportation Traversal
// class Solution {
// public int minMoves(String[] matrix) {
// int m = matrix.length, n = matrix[0].length();
// Map<Character, List<int[]>> g = new HashMap<>();
// for (int i = 0; i < m; i++) {
// String row = matrix[i];
// for (int j = 0; j < n; j++) {
// char c = row.charAt(j);
// if (Character.isAlphabetic(c)) {
// g.computeIfAbsent(c, k -> new ArrayList<>()).add(new int[] {i, j});
// }
// }
// }
// int[] dirs = {-1, 0, 1, 0, -1};
// int INF = Integer.MAX_VALUE / 2;
// int[][] dist = new int[m][n];
// for (int[] arr : dist) Arrays.fill(arr, INF);
// dist[0][0] = 0;
// Deque<int[]> q = new ArrayDeque<>();
// q.add(new int[] {0, 0});
// while (!q.isEmpty()) {
// int[] cur = q.pollFirst();
// int i = cur[0], j = cur[1];
// int d = dist[i][j];
// if (i == m - 1 && j == n - 1) return d;
// char c = matrix[i].charAt(j);
// if (g.containsKey(c)) {
// for (int[] pos : g.get(c)) {
// int x = pos[0], y = pos[1];
// if (d < dist[x][y]) {
// dist[x][y] = d;
// q.addFirst(new int[] {x, y});
// }
// }
// g.remove(c);
// }
// for (int idx = 0; idx < 4; idx++) {
// int a = dirs[idx], b = dirs[idx + 1];
// int x = i + a, y = j + b;
// if (0 <= x && x < m && 0 <= y && y < n && matrix[x].charAt(y) != '#'
// && d + 1 < dist[x][y]) {
// dist[x][y] = d + 1;
// q.addLast(new int[] {x, y});
// }
// }
// }
// return -1;
// }
// }
// Accepted solution for LeetCode #3552: Grid Teleportation Traversal
function minMoves(matrix: string[]): number {
const m = matrix.length,
n = matrix[0].length;
const g = new Map<string, [number, number][]>();
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const c = matrix[i][j];
if (/^[A-Za-z]$/.test(c)) {
if (!g.has(c)) g.set(c, []);
g.get(c)!.push([i, j]);
}
}
}
const dirs = [-1, 0, 1, 0, -1];
const INF = Number.MAX_SAFE_INTEGER;
const dist: number[][] = Array.from({ length: m }, () => Array(n).fill(INF));
dist[0][0] = 0;
const cap = m * n * 2 + 5;
const dq = new Array<[number, number]>(cap);
let l = cap >> 1,
r = cap >> 1;
const pushFront = (v: [number, number]) => {
dq[--l] = v;
};
const pushBack = (v: [number, number]) => {
dq[r++] = v;
};
const popFront = (): [number, number] => dq[l++];
const empty = () => l === r;
pushBack([0, 0]);
while (!empty()) {
const [i, j] = popFront();
const d = dist[i][j];
if (i === m - 1 && j === n - 1) return d;
const c = matrix[i][j];
if (g.has(c)) {
for (const [x, y] of g.get(c)!) {
if (d < dist[x][y]) {
dist[x][y] = d;
pushFront([x, y]);
}
}
g.delete(c);
}
for (let idx = 0; idx < 4; idx++) {
const x = i + dirs[idx],
y = j + dirs[idx + 1];
if (0 <= x && x < m && 0 <= y && y < n && matrix[x][y] !== '#' && d + 1 < dist[x][y]) {
dist[x][y] = d + 1;
pushBack([x, y]);
}
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.