Connection Status:
Competition Arena > GroupedWord
SRM 432 · 2009-01-06 · by nika · Greedy, String Manipulation
Class Name: GroupedWord
Return Type: String
Method Name: restore
Arg Types: (vector<string>)
Problem Statement

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).

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"

Submissions are judged against all 229 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class GroupedWord with a public method string restore(vector<string> parts) · 229 test cases · 2 s / 256 MB per case

Submitting as anonymous