Weekly Problem commonCount


Problem Statement

Two strings have a letter in common if it appears in both strings. The position of letter is not important for it to be counted as common. Once a particular letter is counted as in common, it cannot be counted again. For example, an o appearing twice in one word but only once in the other counts only as one letter in common. However, an o appearing twice both words counts as two letters in common.

Write a function that takes two strings and returns the number of letters they have in common.

Hint: you will need some way to avoid counting a letter more than once if it is in common to both words. To do this, replace each letter with a nonsense character once you have examined it using the String method replace, replaceAll, or replaceFirst.

Definition

Class

public class Jotto { public int commonCount (String a, String b) { // fill in code here } }

Constraints

Examples

(quote for clarity, they're not part of the string)
  1.   "horse" "mirth"
    
    Returns: 2

  2. "horse" "short"
    Returns: 4

  3.   "horse" "moose"
    
    Returns: 3

  4.   "horse" "seems"
    
    Returns: 2

Robert C. Duvall
Last modified: Tue Sep 21 16:37:40 EDT 2004