Connection Status:
Competition Arena > StringCompare
SRM 227 · 2005-01-22 · by erinn · Simple Search, Iteration, String Manipulation
Class Name: StringCompare
Return Type: int
Method Name: simpleDifference
Arg Types: (string, string)
Problem Statement

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 Strings a and b and are to return an int indicating the score (as defined above) of how closely the two match.

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'.
Examples
0)
"TICK"
"TOCK"
Returns: 3

The first example from the problem statement.

1)
"CAT"
"DOG"
Returns: 0

The second example from the problem statement.

2)
"APPLE"
"APPLES"
Returns: 5

Notice the lengths are different, so the most we can compare is 5 characters, which are all identical.

3)
"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.

4)
"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.

Coding Area

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

Submitting as anonymous