LeetCode-in-Java

3963. Create Grid With Exactly One Path

Easy

You are given two integers m and n, representing the number of rows and columns of a grid.

Construct any m x n grid consisting only of the characters '.' and '#', where:

A valid path is a sequence of free cells that:

Return any grid such that there is exactly one valid path from the top-left cell to the bottom-right cell.

Example 1:

Input: m = 2, n = 3

Output: [”..#”,”#..”]

Explanation:

The only valid path is: (0,0) → (0,1) → (1,1) → (1,2)

Example 2:

Input: m = 3, n = 3

Output: [”..#”,”#..”,”##.”]

Explanation:

The only valid path is: (0,0) → (0,1) → (1,1) → (1,2) → (2,2)

Example 3:

Input: m = 1, n = 4

Output: [“….”]

Explanation:

The only valid path is: (0,0) → (0,1) → (0,2) → (0,3)

Constraints:

Solution

public class Solution {
    public String[] createGrid(int m, int n) {
        var res = new String[m];
        var sb = new StringBuilder();
        sb.repeat(".", Math.max(0, n));
        res[0] = sb.toString();
        sb = new StringBuilder();
        sb.repeat("#", Math.max(0, n - 1));
        sb.append(".");
        for (var i = 1; i < m; i++) {
            res[i] = sb.toString();
        }
        return res;
    }
}