TopcoderArchiveVisit Topcoder
SRM22 Jan 2005
SRM 227

StringCompare

Problem statement, definition, constraints, and public examples.

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.

Definition

Class:
StringCompare
Method:
simpleDifference
Parameters:
String, String
Returns:
int
Method signature:
int simpleDifference(String a, String b)
(be sure your method is public)

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

  1. "TICK"
    "TOCK"
    Returns: 3
    The first example from the problem statement.
  2. "CAT"
    "DOG"
    Returns: 0
    The second example from the problem statement.
  3. "APPLE"
    "APPLES"
    Returns: 5
    Notice the lengths are different, so the most we can compare is 5 characters, which are all identical.
  4. "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.
  5. "ANTIDISESTABLISHMENTARIANISM"
    "FLOCCIPAUCINIHILIPIFICATION"
    Returns: 1
Back to all problems