logo Practice-It logo

BJP5 Exercise 11.21: pairCounts

Language/Type: Java Collections Sets and Maps
Author: Jeff Prouty (on 2019/09/19)

Write a method called pairCounts that accepts a list of strings representing individual words and counts the number of occurrences of all 2-character sequences of letters in those words. For example, suppose the list contains ["banana", "bends", "i", "mend", "sandy"]. This list contains the following two-character pairs: "ba", "an", "na", "an", "na" from "banana"; "be", "en", "nd", "ds" from "bends"; "me", "en", "nd" from "mend"; and "sa", "an", "nd", "dy" from "sandy". (Note that "i" is only one character long, so it contains no pairs.) Your method should count the occurrences of these pairs of letters and return the counts as a Map. In this case you would return {an=3, ba=1, be=1, ds=1, dy=1, en=2, me=1, na=2, nd=3, sa=1}. Notice that pairings that occur more than once in the same word should be counted as separate occurrences. For example, "an" and "na" each occur twice in "banana".

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.