TopCoder problem "TheThe" used in TCO04 Qual 5 (Division I Level One)



Problem Statement

    

If I asked you to read the following lines aloud

    It was the best of times, it was the worst of times, it was the
    the age of wisdom, it was the age of foolishness, it was the 
    epoch of belief, it was the epoch of incredulity, ...
there is a very good chance that you would say "it was the age of wisdom" instead of "it was the the age of wisdom". This is a common typing error that can be fiendishly difficult to spot.

After making this error one too many times, you have decided to build a tool to detect such errors automatically. Given a String[] lines, you are to return the zero-based index of the first line whose last word is the same as the first word on the next line, or -1 if there is no such line. Each line contains one or more words separated by spaces. Words are composed of uppercase and lowercase letters ('A'-'Z' and 'a'-'z'). Two words are considered the same if they are exactly equal, including capitalization.

 

Definition

    
Class:TheThe
Method:check
Parameters:String[]
Returns:int
Method signature:int check(String[] lines)
(be sure your method is public)
    
 

Constraints

-lines contains between 2 and 20 elements, inclusive.
-Each element of lines contains between 1 and 50 characters, inclusive.
-Each element of lines contains only letters ('A'-'Z' and 'a'-'z') and spaces (' ').
-No element of lines begins with a space, ends with a space, or contains two adjacent spaces.
 

Examples

0)
    
{ "It was the best of times it was",
  "the worst of times it was the",
  "the age of wisdom it was the age",
  "of foolishness it was the epoch",
  "epoch of belief it was the epoch",
  "of incredulity" }
Returns: 1
The error appears twice, once with the word "the" between lines 1 and 2, and once with the word "epoch" between lines 3 and 4. You are looking for the first such error, so you return 1.
1)
    
{ "I like you",
  "You like me",
  "Lets have dinner" }
Returns: -1
Capitalization matters.
2)
    
{ "Sometimes I call my father Pops",
  "PopsEdit is a feature rich editor",
  "Just once I would like to beat SnapDragon",
  "Dragon stew is tasty but the ingredients",
  "are difficult to come by" }
Returns: -1
Only whole words count as matches, not prefixes or suffixes.
3)
    
{ "A line might contain a",
  "single",
  "word" }
Returns: -1
4)
    
{ "I know that",
  "that kind of food",
  "is bad for me but",
  "I like it anyway" }
Returns: 0
Sometimes you actually intend to repeat a word, but such instances should be flagged as errors anyway.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5877&pm=2969

Writer:

vorthys

Testers:

PabloGilberto , lbackstrom

Problem categories:

Simple Search, Iteration