Common APT

Class

public class Common { public int count (String a, String b) { // TODO: fill in code here } }

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 character that does not normally occur in words (e.g, "@") once you have examined it.

Constraints

Examples

  1. "horse" 
    
    "mirth"
    
    Returns: 2

    h and r in common.

  2. "horse" 
    
    "short"
    
    Returns: 4

    h,o,r, and s in common

  3. "horse"
    
    "seems"
    
    Returns: 2

  4. "horse" 
    
    "moose"
    
    Returns: 3