TopcoderArchiveVisit Topcoder
SRM1 Dec 2004
SRM 221

EqualSubstrings

Problem statement, definition, constraints, and public examples.

Problem Statement

You will be given a String str consisting of lowercase letters. You will return a String[] containing elements x and y in that order. The returned Strings x and y must satisfy:
  • 1) The string xy (x with y concatenated on the end) must equal str.
  • 2) The number of a's in x must equal the number of b's in y.
  • 3) If multiple solutions are possible, use the one that maximizes the length of x.
See the examples for further clarifications.

Definition

Class:
EqualSubstrings
Method:
getSubstrings
Parameters:
String
Returns:
String[]
Method signature:
String[] getSubstrings(String str)
(be sure your method is public)

Constraints

  • str will contain between 1 and 50 characters inclusive.
  • Each character in str will be a lowercase letter ('a'-'z').

Examples

  1. "aaabbb"
    Returns: { "aaa",  "bbb" }
    Here we can split str right down the center.
  2. "bbbaaa"
    Returns: { "bbb",  "aaa" }
    Again the center works.
  3. "bbbbbb"
    Returns: { "bbbbbb",  "" }
    y can be empty.
  4. "aaaaaa"
    Returns: { "",  "aaaaaa" }
    x can be empty.
  5. "abjlkbjalkbjaljsbljbalb"
    Returns: { "abjlkbjalkbjaljs",  "bljbalb" }
    Make sure to maximize the length of x.
Back to all problems