← 返回 waymo 的题目列表Count Same-Color Squares in a Grid
类型:online_judge
Problem: Count Same-Color Squares in a Grid
You are given a 2D grid grid of characters. Each cell contains an uppercase letter (e.g., A/B/C/D) representing a color. The grid size is arbitrary (no fixed limit on rows/columns).
Count how many same-color square regions exist in the grid.
A square region is a contiguous k x k (k >= 1) submatrix such that all cells inside it have the exact same character.
Input (stdin)
The first line contains two integers m n, the number of rows and columns.
The next m lines each contain a string of length n with uppercase letters.
Output (stdout)
Output a single integer: the total number of same-color square regions.
Constraints
1 <= m, n <= 2000 (can be discussed for larger sizes in interview)
Example
Input:
3 4
AABB
AABB
AAAA
Output:
13
Explanation (informal):
All 1x1 cells are squares: 12
There is one 2x2 same-color square at the top-left
No larger same-color squares
Total: 12 + 1 = 13
Example
Input
1 1
A
Output
1