← 返回 uber 的题目列表Replace dashes with nearest letter (tie-break by lexicographical order)
类型:online_judge
Problem: Replace '-' cells with the nearest letter (with tie-breaking)
You are given an m x n character matrix grid. Each cell is either the dash character '-' or an English letter (e.g., 'a'-'z'; exact casing depends on the input).
You must replace every '-' cell using letters from its Manhattan-distance-1 neighbors (4-directional adjacency: up, down, left, right; distance 1 means directly adjacent).
If a '-' cell has one or more adjacent letters, replace it with any adjacent letter.
Follow-up requirement: if there are multiple adjacent letters, you must replace it with the lexicographically smallest letter among them.
You may return the resulting matrix or modify it in-place.
Suggested I/O format
Input: m n followed by m strings of length n.
Output: m strings after replacement.
Constraints (typical interview assumptions)
1 <= m, n <= 2000
Need near O(m*n) time.
Example
Input:
3 4
-a--
----
--b-
One valid output under the follow-up rule:
aaaa
abbb
bbbb
Example
Input
1 1
a
Output
a