Statistics

Problem Statement for "PunctuationCleaner"

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

  1. "This cheese is really great!!!!!"

    Returns: "This cheese is really great!"

  2. "You really like THIS cheese!?!?!?!!!?"

    Returns: "You really like THIS cheese?"

  3. "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! ?"

  4. " a b c A B ! !!C!!! ! ! !C ?!!? ?!? ??"

    Returns: " a b c A B ! !C! ! ! !C ? ? ?"

  5. " !!?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.

  6. "??????????????????"

    Returns: "?"

  7. "?"

    Returns: "?"

  8. "!"

    Returns: "!"

  9. "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

    Returns: "!"

  10. "?!!!!!!!!!!!!!!!!!"

    Returns: "?"

  11. "!!!!!!!!!!!!?"

    Returns: "?"

  12. "!?"

    Returns: "?"

  13. "?!"

    Returns: "?"

  14. "!!"

    Returns: "!"

  15. "Aso!idjf ?owiej !?foj w!?efoji o!!wIEJ"

    Returns: "Aso!idjf ?owiej ?foj w?efoji o!wIEJ"

  16. "! !! !!! !!!! ? ?? ??? ???? !? !!? !?! ?!!"

    Returns: "! ! ! ! ? ? ? ? ? ? ? ?"

  17. "!a !a! a!!! !!!!a b? c?? ?c?? ??C? !!?e !?!f d?!!"

    Returns: "!a !a! a! !a b? c? ?c? ?C? ?e ?f d?"

  18. "ALKJOSDIJFI ABC ZZZ a z aaa zzz"

    Returns: "ALKJOSDIJFI ABC ZZZ a z aaa zzz"

  19. "a"

    Returns: "a"

  20. "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

    Returns: "!"

  21. " !!! ?? a "

    Returns: " ! ? a "

  22. "?!? ?!? ???????????? ?"

    Returns: "? ? ? ?"

  23. "! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! "

    Returns: "! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! "

  24. "?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!"

    Returns: "?"

  25. "??A!EXCLAMATION dkkdkii!?lol! ! ? !a ? a?! ??"

    Returns: "?A!EXCLAMATION dkkdkii?lol! ! ? !a ? a? ?"

  26. "!!!HOORAY FOR THIS SENTENCE!!!"

    Returns: "!HOORAY FOR THIS SENTENCE!"

  27. "test"

    Returns: "test"

  28. "a!!!!!!"

    Returns: "a!"

  29. "!?Go!!! S!!!OC!!?!?!CER !!!"

    Returns: "?Go! S!OC?CER !"

  30. " ?? ?! ?!?!a? ??!!?!ks??! !"

    Returns: " ? ? ?a? ?ks? !"

  31. "?????"

    Returns: "?"

  32. "You really like THIS cheese!!!!??? ?? ?!?!?!!!?"

    Returns: "You really like THIS cheese? ? ?"

  33. " !!?X! ?? This is delicious!!! ??!a!?!"

    Returns: " ?X! ? This is delicious! ?a?"

  34. "ae! ae! ae!! ae!! ae!"

    Returns: "ae! ae! ae! ae! ae!"

  35. "??!X?!!?! A!!! B?? ??C!!?"

    Returns: "?X? A! B? ?C?"

  36. "aaa?????"

    Returns: "aaa?"

  37. "?!?!?!? !? asddd ?!?A?!C?A !!!!??? ???!!! ? ? ! !"

    Returns: "? ? asddd ?A?C?A ? ? ? ? ! !"

  38. "Bleh?!!!!"

    Returns: "Bleh?"

  39. " ??! teste tt? !! ??? !!?! "

    Returns: " ? teste tt? ! ? ? "

  40. "a!!! b!!? c!?! d?!! e!?? f?!? g??! l!!! p???! p???"

    Returns: "a! b? c? d? e? f? g? l! p? p?"

  41. "! !"

    Returns: "! !"

  42. "a!!?!!"

    Returns: "a?"

  43. " !X? "

    Returns: " !X? "

  44. "a?"

    Returns: "a?"

  45. " "

    Returns: " "


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: