TopcoderArchiveVisit Topcoder
SRM22 Nov 2006
SRM 327

FunnyFence

Problem statement, definition, constraints, and public examples.

Problem Statement

A sequence of characters is called a fence if it consists of alternating '|' and '-' characters, like "|-|-|-|" or "-|-|" (quotes for clarity only). Notice that "|-||-|" or "--" are not fences, because each contains two equal characters adjacent to each other. Given a string s, find the longest consecutive substring of it that is a fence, and return its length.

Definition

Class:
FunnyFence
Method:
getLength
Parameters:
String
Returns:
int
Method signature:
int getLength(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character of s will be either '|' or '-'.

Examples

  1. "|-|-|"
    Returns: 5
    The entire string is a fence.
  2. "-|-|-|-"
    Returns: 7
    Still a fence.
  3. "||||||"
    Returns: 1
    A fence can be just 1 character long, so every 1 character substring here is a fence.
  4. "|-||-|-"
    Returns: 4
    The last 4 characters form the longest consecutive substring that is a fence.
  5. "|-|---|-|---|-|"
    Returns: 5
    "-|-|-" right in the middle gives the longest fence.
  6. "|||-||--|--|---|-||-|-|-|--||---||-||-||-|--||"
    Returns: 8
Back to all problems