TopCoder problem "GroupedWord" used in SRM 432 (Division I Level Two)



Problem Statement

    

A word is grouped if, for each letter in the word, all occurrences of that letter form exactly one consecutive sequence. In other words, no two equal letters are separated by one or more letters that are different. For example, the words "ccazzzzbb" and "code" are grouped, while "aabbbccb" and "topcoder" are not.



A grouped word was divided into several parts. You are given all the parts in random order as a String[]. Reconstruct the original word and return it. If there is more than one possible answer, return "MANY" instead. If no grouped word could have resulted in the given parts, return "IMPOSSIBLE" instead (all quotes for clarity).

 

Definition

    
Class:GroupedWord
Method:restore
Parameters:String[]
Returns:String
Method signature:String restore(String[] parts)
(be sure your method is public)
    
 

Constraints

-parts will contain between 1 and 50 elements, inclusive.
-Each element of parts will contain between 1 and 20 characters, inclusive.
-Each element of parts will contain only lowercase letters ('a' - 'z').
 

Examples

0)
    
{"aaa", "a", "aa"}
Returns: "aaaaaa"
These parts could only have come from the word "aaaaaa", which is a grouped word.
1)
    
{"ab", "bba"}
Returns: "IMPOSSIBLE"
The only possible original words are "abbba" and "bbaab", and neither of them are grouped words.
2)
    
{"te", "st"}
Returns: "stte"
3)
    
{"te", "s", "t"}
Returns: "MANY"
The initial word could be either "stte" or "ttes".
4)
    
{"orr", "rd", "woo", "www"}
Returns: "wwwwooorrrd"
5)
    
{"abcb"}
Returns: "IMPOSSIBLE"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13694&pm=10153

Writer:

nika

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

Greedy, String Manipulation