TopCoder problem "PunctuationCleaner" used in SRM 376 (Division II Level One)



Problem Statement

    

Some writers like to super-emphasize certain points by using multiple exclamation points instead of one. For example, "This cheese is really great!!! I like exclamation points!!!!!!!!" Other times, writers express surprise by mixing exclamation points and question marks. For example, "You really like THIS cheese!?!?!?!!!?".

 

You are editing a document for publication, and you'd like to get rid of some of the extra punctuation. Wherever you see a group of multiple consecutive exclamation points, replace it with a single exclamation point. Wherever you see a group of multiple consecutive punctuation characters containing at least one question mark and zero or more exclamation points (and no other characters), replace that group with a single question mark. See the examples for further clarification. Return the string after these changes have been made.

 

Definition

    
Class:PunctuationCleaner
Method:clearExcess
Parameters:String
Returns:String
Method signature:String clearExcess(String document)
(be sure your method is public)
    
 

Constraints

-document will contain between 1 and 50 characters, inclusive.
-document will contain only letters ('a'-'z', 'A'-'Z'), spaces (' '), question marks ('?'), and exclamation points ('!').
 

Examples

0)
    
"This cheese is really great!!!!!"
Returns: "This cheese is really great!"
1)
    
"You really like THIS cheese!?!?!?!!!?"
Returns: "You really like THIS cheese?"
2)
    
" !!?X! ?? This is delicious!!! ??!a!?!"
Returns: " ?X! ? This is delicious! ?a?"
Sometimes writers get very carried away. Remember to preserve spaces and other characters from the input.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10796&pm=8378

Writer:

jmzero

Testers:

PabloGilberto , Olexiy , misof , ivan_metelsky

Problem categories:

String Manipulation