Problem · String

Add Drama to Text Groups

Learn this problem
EasyUpstart logoUpstartFULLTIMEOA

Problem statement

Transform a string s in two steps:

  1. Replace every period . with an exclamation mark !.
  2. Treat every maximal non-space run as one group and append one additional ! after that group.

Preserve every space exactly, including leading, trailing, and repeated spaces. Existing exclamation marks are unchanged before the group-ending mark is appended. Therefore a group whose last character is a period or exclamation mark can end with multiple exclamation marks.

Function

addDrama(s: String) → String

Examples

Example 1

s = "Hello. world"return = "Hello!! world!"

The period becomes !, then one additional ! is appended after each of the groups Hello! and world.

Example 2

s = "Wait... now!"return = "Wait!!!! now!!"

Three periods become three exclamation marks. The group-ending operation adds a fourth after Wait!!! and a second after now!.

Example 3

s = "  a.b  c  "return = "  a!b!  c!  "

The leading, repeated, and trailing spaces are preserved. The two non-space groups each receive one final !.

Constraints

  • 0 <= s.length <= 200000
  • s contains printable ASCII characters and spaces.
  • A space is the only group separator.

More Upstart problems

drafts saved locally
public String addDrama(String s) {
    // Write your code here.
}
s"Hello. world"
expected"Hello!! world!"
checking account