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
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
"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
"OIL"
"PFJMFWXRQYXBNJNTWIZVSRJIKPGIWEWN"
Returns: 0
"UCWHELOLFDGGPYKMZ"
"CGHEAXJAMRSMDDNOJEFAOFVPAIVNSGJG"
Returns: 0
"ZLOGAPUZTOHKFQGKSRSNFYVVQTIQMRJBSVOY"
"J"
Returns: 0
"FYLI"
"WFSKQDNKTRH"
Returns: 0
"XZJC"
"COOGCEZEXKOZZQMKLLTTIL"
Returns: 0
"DHODY"
"RYJSGPJWLIUIH"
Returns: 0
"SWGDCMBWUWONPVFLMDICPSC"
"WVKUDUGHVREBFMZEQLXOKMRKJIHNKGNA"
Returns: 0
"QCJNMXMDDLEAQVAZRLDXLOK"
"MQBCMDFZYOXENRKLX"
Returns: 1
"JVABWPF"
"DHWKWEGKQWPLOFK"
Returns: 1
"JKBEMNPFOYWHCJT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