Skip to main content
Back to problems
#2639
Easy Algorithms

Find the width of columns of a grid

Array Matrix
70.4% acceptance
Feb 25, 2026
194
54
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 is 3 since -10 is of length 3. 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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
    let m = grid.len();
    let n = grid[0].len();

    fn int_len(x: i32) -> i32 {
      let s = x.to_string();
      s.len() as i32
    }

    let mut ans = vec![0i32; n];
    for i in 0..m {
      for j in 0..n {
        let w = int_len(grid[i][j]);
        if w > ans[j] {
          ans[j] = w;
        }
      }
    }
    ans
  }
}