TopcoderArchive
Archive
Algorithm problem

CyclicSequence

Problem statement, definition, constraints, and public examples.

Problem Statement

Given a string s of length n, one can generate an infinite cyclical sequence a0, a1, ... as follows. For each non-negative integer i, compute a zero-based index j as j = i%n. ai is equal to the j-th character of s.
You are given a String s. Return the shortest string which generates the same sequence as s.

Definition

Class:
CyclicSequence
Method:
minimalCycle
Parameters:
String
Returns:
String
Method signature:
String minimalCycle(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be a digit ('0'-'9').

Examples

  1. "11111"
    Returns: "1"
    The sequence generated by this string is ai=1, which can be generated by the string "1".
  2. "234234234"
    Returns: "234"
  3. "567890"
    Returns: "567890"
  4. "0"
    Returns: "0"
  5. "678967"
    Returns: "678967"
Back to all problems