TopCoder problem "StringCompare" used in SRM 227 (Division II Level One)



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

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

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=3532

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=6516&pm=3532

Writer:

timmac

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Simple Search, Iteration, String Manipulation