Find the Width of Columns of a Grid
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers.
- For example, if
grid = [[-10], [3], [12]], the width of the only column is3since-10is of length3.
Return an integer array ans of size n where ans[i] is the width of the ith column.
The length of an integer x with len digits is equal to len if x is non-negative, and len + 1 otherwise.
Examples
Example 1
Input: grid = [[1],[22],[333]]
Output: [3]
Explanation: In the 0th column, 333 is of length 3.
Example 2
Input: grid = [[-15,1,3],[15,7,12],[5,6,-2]]
Output: [3,1,2]
Explanation:
In the 0th column, only -15 is of length 3.
In the 1st column, all integers are of length 1.
In the 2nd column, both 12 and -2 are of length 2.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 100-10^9 <= grid[r][c] <= 10^9
Solution
Method 1 – Column-wise Maximum String Length
Intuition
The width of a column is determined by the maximum length of the string representation of any integer in that column. For each column, check all rows and find the maximum string length.
Approach
- For each column index, iterate through all rows.
- For each value, convert it to a string and compute its length (including the minus sign for negatives).
- Track the maximum length for each column.
- Return the list of maximum lengths for all columns.
Code
C++
class Solution {
public:
vector<int> findColumnWidth(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> ans(n, 0);
for (int j = 0; j < n; ++j) {
for (int i = 0; i < m; ++i) {
int len = to_string(grid[i][j]).size();
ans[j] = max(ans[j], len);
}
}
return ans;
}
};
Go
func findColumnWidth(grid [][]int) []int {
m, n := len(grid), len(grid[0])
ans := make([]int, n)
for j := 0; j < n; j++ {
for i := 0; i < m; i++ {
l := len(strconv.Itoa(grid[i][j]))
if l > ans[j] {
ans[j] = l
}
}
}
return ans
}
Java
class Solution {
public int[] findColumnWidth(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] ans = new int[n];
for (int j = 0; j < n; ++j) {
for (int i = 0; i < m; ++i) {
int len = Integer.toString(grid[i][j]).length();
ans[j] = Math.max(ans[j], len);
}
}
return ans;
}
}
Kotlin
class Solution {
fun findColumnWidth(grid: Array<IntArray>): IntArray {
val m = grid.size
val n = grid[0].size
val ans = IntArray(n)
for (j in 0 until n) {
for (i in 0 until m) {
val len = grid[i][j].toString().length
ans[j] = maxOf(ans[j], len)
}
}
return ans
}
}
Python
class Solution:
def findColumnWidth(self, grid: list[list[int]]) -> list[int]:
m, n = len(grid), len(grid[0])
ans = [0] * n
for j in range(n):
for i in range(m):
ans[j] = max(ans[j], len(str(grid[i][j])))
return ans
Rust
impl Solution {
pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
let m = grid.len();
let n = grid[0].len();
let mut ans = vec![0; n];
for j in 0..n {
for i in 0..m {
let len = grid[i][j].to_string().len() as i32;
ans[j] = ans[j].max(len);
}
}
ans
}
}
TypeScript
class Solution {
findColumnWidth(grid: number[][]): number[] {
const m = grid.length, n = grid[0].length;
const ans = Array(n).fill(0);
for (let j = 0; j < n; ++j) {
for (let i = 0; i < m; ++i) {
ans[j] = Math.max(ans[j], grid[i][j].toString().length);
}
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(mn)— Each cell is checked once. - 🧺 Space complexity:
O(n)— For the output array.