TopcoderArchive
Archive
Algorithm problem

MultiWordSearch

Problem statement, definition, constraints, and public examples.

Problem Statement

A word search consists of a rectangular grid of letters. Given a grid, the goal is to find a word, in this case oriented horizontally (left to right) or vertically (top to bottom). The grid of letters is given in String[] grid, where each elemnt represents a row of the grid, with row 0 being at the top.

Given a String word to find, how many times does it appear in grid, either horizontally or vertically?

Definition

Class:
MultiWordSearch
Method:
countTimes
Parameters:
String[], String
Returns:
int
Method signature:
int countTimes(String[] grid, String word)
(be sure your method is public)

Constraints

  • word will contain between 2 and 50 characters, inclusive.
  • Each character of word will be an upper-case letter ('A'-'Z').
  • grid will contain between 1 and 50 elements, inclusive.
  • Each element of grid will contain between 1 and 50 characters, inclusive.
  • All elements of grid will be the same length.
  • Each character of each element of grid will be an upper-case letter ('A'-'Z').

Examples

  1. {"WORD",
     "ORDW",
     "RDWO",
     "DWOR"}
    "WORD"
    Returns: 2
    The word appears once horizontally and once vertically.
  2. {"ABCDE",
     "FGHIJ",
     "KLMNO",
     "PQRST"}
    "XYZ"
    Returns: 0
    The word doesn't appear at all.
  3. {"ABA",
     "BAB",
     "ABA"}
    "AB"
    Returns: 6
    There are three horizontal occurrences, and three vertical.
  4. {"AAA",
     "AAA",
     "AAA"}
    "AA"
    Returns: 12
Back to all problems