TopcoderArchiveVisit Topcoder
SRM4 Dec 2016
SRM 703

AlternatingString

Problem statement, definition, constraints, and public examples.

Problem Statement

A string of zeros and ones is called an alternating string if no two adjacent characters are the same. Examples of alternating strings: "1", "10101", "0101010101".

You are given a String s. Each character of s is a '0' or a '1'. Please find the longest contiguous substring of s that is an alternating string. Return the length of that substring.

Definition

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

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be '0' or '1'.

Examples

  1. "111101111"
    Returns: 3
    Among all substrings, there are 5 different alternating strings: "1", "0", "10", "01", "101". The one with maximal length is "101" and the length is 3.
  2. "1010101"
    Returns: 7
    The string s itself is an alternating string.
  3. "000011110000"
    Returns: 2
    Note that a substring must be contiguous. The longest alternating substrings of this s are "01" and "10". The string "010" is not a substring of this s.
  4. "1011011110101010010101"
    Returns: 8
  5. "0"
    Returns: 1
Back to all problems