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.
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
"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"
"09999999x99xyyz"
"9999999x00099x2yyz"
Returns: 10000100
"abccccd3d2ddd4d4efg"
"abcccc5d7d4e"
Returns: 22
"abccccd3d2ddd4d4efg"
"abcccc5d7d4ef"
Returns: 23
"abccccd3d2ddd4d4efg"
"ab1c1ccc5d7d4efg"
Returns: -1
"0a00b000c"
"abc"
Returns: 0
"3ab"
"ababab"
Returns: 1
"12345678a12345678a12345678ab"
"37037034ab"
Returns: -1
"3a0b2cddd"
"aaabccd"
Returns: 3
"a"
"0a"
Returns: 0
"a"
"2a"
Returns: 1
"a0a0a0a0a0a0a0a0a0a0a0a0a0a0aa"
"2a"
Returns: -1
"87436a34b22j3ckj34has"
"87436a34b22j3ckj00000034has"
Returns: -1
"87436a34b22j3ckj34has"
"87436a34b2j2j2j2j2j2j2j2j2j2j2j3ckj34has"
Returns: -1
"87436a34b22j3ckj34has"
"87436a34b2j2j2j2j2j020j2j2j2j2j2j3ckj34has"
Returns: 87492
"87436a34b22j3ckj34has0s0h"
"87436a34b2j2j2j2j2j02j2j2j2j2j2j3ckj34has"
Returns: -1
"87436a34b22j3ckj34has0h"
"87436a34b2j2j2j2j2j02j2j2j2j2j2j3ckj34has"
Returns: -1
"ab"
"abc"
Returns: 2
"a3bc23767h"
"a3bc232h"
Returns: 237
"3a"
"0b3a"
Returns: -1
"0a0b"
"0c"
Returns: -1
"a"
"b"
Returns: 0
"99999999aa99999999aa99999999aa99999999aa99999999a"
"99999999aa99999999aa99999999aa99999999aa99999999aa"
Returns: 499999999
"0abbc"
"0bbbd"
Returns: 2
"10000a"
"100a"
Returns: 100
"5a"
"6a"
Returns: 5
"3a3c3d"
"0baaa0bccc0bddd"
Returns: -1
"aaa0baaa"
"6a"
Returns: -1
"a0ba"
"aa"
Returns: -1
"20a30ab"
"50ac"
Returns: 50
"0a0b00a1a"
"0b0a0a1a"
Returns: -1