TopcoderArchive
Archive
Algorithm problem

SquareCounting

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a grid of characters, where each element describes a row. A solid square is a contiguous n x n section of characters, all of which have the same value. Return the number of solid squares in grid that aren't contained in larger solid squares.

Definition

Class:
SquareCounting
Method:
howMany
Parameters:
String[]
Returns:
int
Method signature:
int howMany(String[] grid)
(be sure your method is public)

Constraints

  • grid will contain between 1 and 10 elements, inclusive.
  • Each element of grid will contain between 1 and 10 characters, inclusive.
  • Each element grid will contain the same number of characters.
  • Each character in grid will be an uppercase letter ('A'-'Z').

Examples

  1. {"AB",
     "CD"}
    Returns: 4
    Here we have 4 separate 1 x 1 squares.
  2. {"AAB",
     "AAB",
     "BBB"}
    Returns: 6
    Here we have a 2 x 2 square of 'A's and 5 squares of 'B's (each 1 x 1). Thus we return 6.
  3. {"AA",
     "AA",
     "AA"}
    Returns: 2
    The two possible 2 x 2 squares are counted.
  4. {"AAA",
     "AAA",
     "AAA"}
    Returns: 1
    Only the large 3x3 square is countable.
  5. {
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA",
    "AAAAAAAAAA"
    }
    
    Returns: 1
  6. {
    "AAAAAABBB",
    "AAAAAABBB",
    "AAAAAABBB",
    "AAAAAABBB",
    "AAAAAABBB",
    "BBBAAAAAA",
    "BBBAAAAAA",
    "BBBAAAAAA",
    "BBBAAAAAA",
    "BBBAAAAAA"
    }
    Returns: 12
Back to all problems