TopCoder problem "DigramAnalysis" used in Delta Round 3 (Division I Level Two)



Problem Statement

    

A word is a maximal continuous sequence of letters ('a'-'z'). Words are separated by spaces.

A digram is a string of length two. A word is said to contain digram "xy" if it contains an 'x'" immediately followed by 'y'. For example, the word "coder" contains digram "od" but doesn't contain digram "oe".

Consider text that consists of one or more words. The most frequent digram in text is the one that is contained in maximal number of words.

Given a piece of text, the most frequent digram is defined as the one that occurs in the greatest number of words. You are given a String[] chunks. Concatenate all elements of chunks to produce one long String of text. Return the most frequent digram in this text. If there are multiple possible answers, return the one among them that comes earliest alphabetically.

 

Definition

    
Class:DigramAnalysis
Method:mostFrequent
Parameters:String[]
Returns:String
Method signature:String mostFrequent(String[] chunks)
(be sure your method is public)
    
 

Constraints

-chunks will contain between 1 and 50 elements, inclusive.
-Each element of chunks will contain between 1 and 50 characters, inclusive.
-Each element of chunks will contain only lowercase letters ('a'-'z') and spaces.
-There will be at least one letter in at least one element of chunks.
 

Examples

0)
    
{"coder decoder"}
Returns: "co"
1)
    
{"abracadabra cat"}
Returns: "ca"
Note that although "ab" is contained twice in "abracadabra", it is contained in only one word.
2)
    
{"cat mouse mouse mouse"}
Returns: "mo"
Each occurence of "mouse" is counted separately, so "mo" is contained in three words here.
3)
    
{"i", "i"}
Returns: "ii"
Note that the elements of chunks must be concatenated before analyzing.
4)
    
{" hello world "}
Returns: "el"
Note that there can be spaces before the first word and after the last one.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7553

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10721&pm=7553

Writer:

andrewzta

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Search, Iteration, String Manipulation