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
"|-|-|"
Returns: 5
The entire string is a fence."-|-|-|-"
Returns: 7
Still a fence."||||||"
Returns: 1
A fence can be just 1 character long, so every 1 character substring here is a fence."|-||-|-"
Returns: 4
The last 4 characters form the longest consecutive substring that is a fence."|-|---|-|---|-|"
Returns: 5
"-|-|-" right in the middle gives the longest fence."|||-||--|--|---|-||-|-|-|--||---||-||-||-|--||"
Returns: 8