StringCmp
SRM 124 · 2002-12-12 · by dgoodman
Problem Statement
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.
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
"98765432aa" "98765433a" Returns: -1
The two represented strings are identical. Each has length = 98,765,433.
"0c" "00d" Returns: -1
The two represented strings are both empty strings
"543e" "0f" Returns: 0
The empty string is shorter. Its length is 0.
"0a0b00c" "a2b0x2b1c" Returns: 0
"13a10bc" "3a5aa3a1a2b0c2b3c" Returns: 17
"a3bc" "ab04c" Returns: 2
The two represented strings are "abbbc" and "abcccc"
Submissions are judged against all 36 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class StringCmp with a public method int firstDif(string code1, string code2) · 36 test cases · 2 s / 256 MB per case