TopcoderArchive
Archive
Algorithm problem

StringManipulation

Problem statement, definition, constraints, and public examples.

Problem Statement

You are to manipulate a given String text, which may contain so-called manipulators. Manipulators are the characters '#' , '$' and '@'. Scan the string from left to right, and whenever you encounter a manipulator, consider the substring between it and the previously encountered manipulator (excluding both manipulators). If there is no previous manipulator, use the beginning of the string instead. Perform one of the following operations on that substring:

  • If the manipulator is '#', reverse the substring.
  • If the manipulator is '@', perform a circular rotation of the substring one position to the right.
  • If the manipulator is '$', perform a circular rotation of the substring one position to the left.

All manipulators in the string should be erased after all manipulations are accomplished. You are to return the resulting string.

Definition

Class:
StringManipulation
Method:
manipulate
Parameters:
String
Returns:
String
Method signature:
String manipulate(String text)
(be sure your method is public)

Constraints

  • text will contain between 1 and 50 characters, inclusive.
  • text will contain only lowercase letters ('a'-'z'), '#', '@' and '$' characters.
  • No two manipulators will be adjacent in text.
  • The first character of text will not be '@', '#' or '$'.

Examples

  1. "rb"
    Returns: "rb"
    You do nothing because no manipulator is encountered.
  2. "qh$"
    Returns: "hq"
    You just need to rotate the substring "qh" one position to the left.
  3. "l#esr@"
    Returns: "lres"
    When the '#' is encountered, you reverse the substring "l". When the '@' is encountered, you rotate the substring "esr" one position to the right.
  4. "xz#dwb#wls@eg@mqdu"
    Returns: "zxbwdswlgemqdu"
Back to all problems