Statistics

Problem Statement for "StringCompare"

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

  6. "OIL"

    "PFJMFWXRQYXBNJNTWIZVSRJIKPGIWEWN"

    Returns: 0

  7. "UCWHELOLFDGGPYKMZ"

    "CGHEAXJAMRSMDDNOJEFAOFVPAIVNSGJG"

    Returns: 0

  8. "ZLOGAPUZTOHKFQGKSRSNFYVVQTIQMRJBSVOY"

    "J"

    Returns: 0

  9. "FYLI"

    "WFSKQDNKTRH"

    Returns: 0

  10. "XZJC"

    "COOGCEZEXKOZZQMKLLTTIL"

    Returns: 0

  11. "DHODY"

    "RYJSGPJWLIUIH"

    Returns: 0

  12. "SWGDCMBWUWONPVFLMDICPSC"

    "WVKUDUGHVREBFMZEQLXOKMRKJIHNKGNA"

    Returns: 0

  13. "QCJNMXMDDLEAQVAZRLDXLOK"

    "MQBCMDFZYOXENRKLX"

    Returns: 1

  14. "JVABWPF"

    "DHWKWEGKQWPLOFK"

    Returns: 1

  15. "JKBEMNPFOYWHCJTLMYWRESHPDBSYNXRVPNHWDEXT"

    "OHZCYXXCCPYUQGGNZZFLWKTOTIJUDLUN"

    Returns: 0

  16. "VLIXLURSQUYXQYRVZMKIHZMNWZYINMSXUGRZIVTVIZP"

    "PUIUPSRDXKOOYTTHYTVWIUZYFZRZVEYNOWZLIWZUWHPVNWYUK"

    Returns: 5

  17. "YFYSVROUTXXSRSRQTWGVVXJZYXVKWTOU"

    "FVRUTIWQTQCKVKVRXZZUTMYRHYRNYVVNLQAYIX"

    Returns: 1

  18. "TVLMCVVXKUVNYVPCTZVNRSRUTYWYPFUGTUYXE"

    "OYLWYLQKWTXWXNPNZUZRTWLMYUYVQQNVN"

    Returns: 2

  19. "HRQQNXVUVYFIZJRXZQSNQUYNJOQ"

    "WSYUPMFUSUSQVYMRNZOQLJSFZIYLXYVMRXSTUSTQL"

    Returns: 1

  20. "REPBIZQVVSHBMWZTITQZHXSRNGOUWRNOXVNWR"

    "QMZNLRSIQZYSSWPBYPTPZZORXZYRZRH"

    Returns: 3

  21. "LRPYHYJVPITLUWNSUQVTLDKGY"

    "YUYTWYYNKRMUYKWZOXOOUZWSXXRKZYSRLM"

    Returns: 1

  22. "SMTRUVKMMSMWRRRGZLDPTNNOWUUUHERXVOVDQLKTZ"

    "SOSNOQZWPPSPJNOWYPLVSWRXYSSZUXPTVSZFTYXHTYMP"

    Returns: 2

  23. "YIOPWVQRKMRXFXNWRWSQOWHOIZZQYVTTVUZVW"

    "TTSRELUOQTFSTOWWUWRVLRSVMSLNSTRSPYOTP"

    Returns: 2

  24. "MXVPIIPGZZZGTRTQVKUQZQWUXTHVIYLWZOOMLYWO"

    "KQPZVXYRHHSSSSSCVFZRXONXUUOXPSWTPMWJWWTYOXYOZS"

    Returns: 1

  25. "ZVSGYYSXXWYXSYQZZQVSVGKJZWLWZHSQVSWAYPTDDSQXWW"

    "VNNGVDHXLXPQNEQOWNHIZZWMTRYQTL"

    Returns: 3

  26. "A"

    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

    Returns: 1

  27. "DLKSDJSDSDGDDSFDSFSDFSDFSDFSGDS"

    "A"

    Returns: 0

  28. "TICKKKK"

    "TOCK"

    Returns: 3

  29. "AAA"

    "AAB"

    Returns: 2

  30. "FAAAAAAAAAAAAAAAAAAAA"

    "A"

    Returns: 0

  31. "A"

    "BBB"

    Returns: 0

  32. "ASA"

    "DSA"

    Returns: 2

  33. "ASAW"

    "DSAI"

    Returns: 2

  34. "CDE"

    "ABCDEFGH"

    Returns: 0

  35. "AAAAAAAAAAAAAAAAAAAAA"

    "A"

    Returns: 1

  36. "TIKOOOOOHFMKHFHJGFVGVBDFDGFDFDGJDFGDJKGHJGHKDGMBNM"

    "TOCK"

    Returns: 1

  37. "ABCCDEFGHIJKLMNOPQ"

    "A"

    Returns: 1

  38. "AAAAAAAAAAAA"

    "A"

    Returns: 1

  39. "TICK"

    "I"

    Returns: 0

  40. "Z"

    "ABCDEFGH"

    Returns: 0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: