TopcoderArchiveVisit Topcoder
SRM12 Dec 2002
SRM 124

StringCmp

Problem statement, definition, constraints, and public examples.

Problem Statement

We can try to compress the representation of a string of letters with a Run Length Encoding. This encoding represents a repeated character with a digit sequence giving the number of repetitions followed by the character. For example, "xb32c5d" represents a string which has 39 characters ("xbccc...cddddd"). A letter that is not immediately preceded by a digit sequence represents just that single character.

We want to be able to compare two Run Length Encodings to see if they represent the same string. Create a class StringCmp that contains the method firstDif that takes two encodings as inputs and returns the first index where the represented strings differ. If the represented strings are identical, return -1.

Definition

Class:
StringCmp
Method:
firstDif
Parameters:
String, String
Returns:
int
Method signature:
int firstDif(String code1, String code2)
(be sure your method is public)

Notes

  • "34ab", "1a0x033ab" and "a33ab" all represent the same String
  • return a 0-based index (if they differ in the first position, return 0)
  • if the strings match up to the point where one string continues but the other ends, return the length of the shorter one

Constraints

  • code1 contains between 1 and 50 characters inclusive
  • code2 contains between 1 and 50 characters inclusive
  • code1 and code2 contain only digits and lowercase letters 'a'-'z'
  • the last character in code1 is a letter
  • the last character in code2 is a letter
  • neither code1 nor code2 contains a sequence of more than 8 consecutive digits

Examples

  1. "98765432aa"
    "98765433a"
    Returns: -1
    The two represented strings are identical. Each has length = 98,765,433.
  2. "0c"
    "00d"
    Returns: -1
    The two represented strings are both empty strings
  3. "543e"
    "0f"
    Returns: 0
    The empty string is shorter. Its length is 0.
  4. "a3bc"
    "ab04c"
    Returns: 2
    The two represented strings are "abbbc" and "abcccc"
Back to all problems