Connection Status:
Competition Arena > PunctuationCleaner
SRM 376 · 2007-11-14 · by jmzero · String Manipulation
Class Name: PunctuationCleaner
Return Type: String
Method Name: clearExcess
Arg Types: (string)
Problem Statement

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.

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)
"a??a ?!a a!?b b!?!C C?!!D D?!?EE ??? FF!!! !???!"
Returns: "a?a ?a a?b b?C C?D D?EE ? FF! ?"
3)
"  a  b  c  A  B ! !!C!!!  ! ! !C ?!!? ?!? ??"
Returns: "  a  b  c  A  B ! !C!  ! ! !C ? ? ?"
4)
" !!?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.

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

Coding Area

Language: C++17 · define a public class PunctuationCleaner with a public method string clearExcess(string document) · 45 test cases · 2 s / 256 MB per case

Submitting as anonymous