Statistics

Problem Statement for "StringCmp"

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. "0a0b00c"

    "a2b0x2b1c"

    Returns: 0

  5. "13a10bc"

    "3a5aa3a1a2b0c2b3c"

    Returns: 17

  6. "a3bc"

    "ab04c"

    Returns: 2

    The two represented strings are "abbbc" and "abcccc"

  7. "09999999x99xyyz"

    "9999999x00099x2yyz"

    Returns: 10000100

  8. "abccccd3d2ddd4d4efg"

    "abcccc5d7d4e"

    Returns: 22

  9. "abccccd3d2ddd4d4efg"

    "abcccc5d7d4ef"

    Returns: 23

  10. "abccccd3d2ddd4d4efg"

    "ab1c1ccc5d7d4efg"

    Returns: -1

  11. "0a00b000c"

    "abc"

    Returns: 0

  12. "3ab"

    "ababab"

    Returns: 1

  13. "12345678a12345678a12345678ab"

    "37037034ab"

    Returns: -1

  14. "3a0b2cddd"

    "aaabccd"

    Returns: 3

  15. "a"

    "0a"

    Returns: 0

  16. "a"

    "2a"

    Returns: 1

  17. "a0a0a0a0a0a0a0a0a0a0a0a0a0a0aa"

    "2a"

    Returns: -1

  18. "87436a34b22j3ckj34has"

    "87436a34b22j3ckj00000034has"

    Returns: -1

  19. "87436a34b22j3ckj34has"

    "87436a34b2j2j2j2j2j2j2j2j2j2j2j3ckj34has"

    Returns: -1

  20. "87436a34b22j3ckj34has"

    "87436a34b2j2j2j2j2j020j2j2j2j2j2j3ckj34has"

    Returns: 87492

  21. "87436a34b22j3ckj34has0s0h"

    "87436a34b2j2j2j2j2j02j2j2j2j2j2j3ckj34has"

    Returns: -1

  22. "87436a34b22j3ckj34has0h"

    "87436a34b2j2j2j2j2j02j2j2j2j2j2j3ckj34has"

    Returns: -1

  23. "ab"

    "abc"

    Returns: 2

  24. "a3bc23767h"

    "a3bc232h"

    Returns: 237

  25. "3a"

    "0b3a"

    Returns: -1

  26. "0a0b"

    "0c"

    Returns: -1

  27. "a"

    "b"

    Returns: 0

  28. "99999999aa99999999aa99999999aa99999999aa99999999a"

    "99999999aa99999999aa99999999aa99999999aa99999999aa"

    Returns: 499999999

  29. "0abbc"

    "0bbbd"

    Returns: 2

  30. "10000a"

    "100a"

    Returns: 100

  31. "5a"

    "6a"

    Returns: 5

  32. "3a3c3d"

    "0baaa0bccc0bddd"

    Returns: -1

  33. "aaa0baaa"

    "6a"

    Returns: -1

  34. "a0ba"

    "aa"

    Returns: -1

  35. "20a30ab"

    "50ac"

    Returns: 50

  36. "0a0b00a1a"

    "0b0a0a1a"

    Returns: -1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: