Problem Statement
You will be given a 2-dimensional grid of letters and a length. Write a method to count the total number of distinct paths of consecutive letters of the given length, starting at 'A'. Paths can step from one square in the grid to any adjacent square (horizontally, vertically, or diagonally).
For example, in the following grid, there are 7 paths of consecutive letters from 'A' to 'C':
{ "ABC",
"CBZ",
"CZC",
"BZZ",
"ZAA" }
A B C A . C A B . A . . A . . A . . . . .
. . . . B . C . . C B . . B . . B . . . .
. . . . . . . . . . . . C . . . . C C . .
. . . . . . . . . . . . . . . . . . B . .
. . . . . . . . . . . . . . . . . . . A .
(spaces are for clarity only)
so, for this grid and a length of 3, your method should return 7.
If there are more than 1,000,000,000 paths, your method should return 1,000,000,000.
Definition
- Class:
- AlphabetCount
- Method:
- count
- Parameters:
- String[], int
- Returns:
- int
- Method signature:
- int count(String[] grid, int length)
- (be sure your method is public)
Constraints
- grid will contain between 1 and 50 elements, inclusive.
- Each element of grid will be between 1 and 50 characters long, inclusive.
- Each element of grid will have the same length.
- grid will contain only uppercase letters ('A'-'Z').
- length will be between 1 and 26, inclusive.
Examples
{ "ABC", "CBZ", "CZC", "BZZ", "ZAA" }3
Returns: 7
This is the example from the problem statement.{ "AAAA", "AAAA", "AAAA" }1
Returns: 12
{ "ABAB", "BABA", "ABAB", "BABA" }2
Returns: 24
{ "HIJKLMNOPQZZZONMLKHIDZYQR", "GYXWVUTSRASTZZPSTUJGECPXS", "FZABCDEFARQPUQRAAAVWFBOWT", "EONMJIHGAJMNOVAAAAAYXANUV", "DCBLKDEFIEKLEDWAAAZFGHMLK", "UVAZYBCGHFDFCAYXNPQZEDIJA", "TSWXAKLZGCZBGZIJOMZRUTCBZ", "RQPONMJIHBAZZHZZKLZZSVWXY" }26
Returns: 7
{ "CZC", "ZBZ", "AZA" }3
Returns: 4
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }26
Returns: 1000000000
{ "BDBCBACABDDCCADCBDDCBDDDBCCCCABACADDDCCCBADDDBADCA", "DCBBBACBDBACCADABCCAABACDBADBCBBABACBCADADCBDABBBD" }4
Returns: 20