StringCompare
SRM 227 · 2005-01-22 · by erinn
Problem Statement
Your company is writing a spell-checker system, and you have been tasked with writing a function to determine how closely two words resemble each other. The algorithm you are to use, albeit not a very good one, is to compare the two words character by character, and count how many times the characters in a given position are the same. For instance, the words "TICK" and "TOCK" have a score of 3, since three characters (T, C, K) are the same. Similarly, "CAT" and "DOG" score 0, since no letters match.
You are given
Notes
- The two strings may have different lengths. In that case, your comparison should only process characters until it reaches the end of either string.
Constraints
- a and b will each contain between 1 and 50 characters, inclusive.
- Each character of a and b will be 'A'-'Z'.
"TICK" "TOCK" Returns: 3
The first example from the problem statement.
"CAT" "DOG" Returns: 0
The second example from the problem statement.
"APPLE" "APPLES" Returns: 5
Notice the lengths are different, so the most we can compare is 5 characters, which are all identical.
"FANTASTIC" "ANTASTIC" Returns: 0
Here's an example of why this particular method is far from ideal. In a situation like this, it appears one character is missing the from the second string, but by our algorithm as described, they score a 0 in similarlity.
"ANTIDISESTABLISHMENTARIANISM" "FLOCCIPAUCINIHILIPIFICATION" Returns: 1
Submissions are judged against all 40 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class StringCompare with a public method int simpleDifference(string a, string b) · 40 test cases · 2 s / 256 MB per case