TopCoder problem "bloggoShortcuts" used in SRM 214 (Division II Level One)

Problem Statement


You are helping to develop a weblog-management system called bloggo. Although bloggo pushes all content to the front end of a website in HTML, not all content authors enjoy using HTML tags in their text. To make their lives easier, bloggo offers a simple syntax called shortcuts to achieve some HTML textual effects. Your job is to take a document written with shortcuts and translate it into proper HTML.

One shortcut is used to make italicized text. HTML does this with the <i> and </i> tags, but in bloggo, an author can simply enclose a piece of text using two instances of the underscore character, '_'. Thus, where a content author writes

  You _should_ see the baby elephant at the zoo!

bloggo will publish the following instead.

  You <i>should</i> see the baby elephant at the zoo!

Another shortcut serves to render text in boldface, which HTML accomplishes with <b> and </b> tags. Bloggo lets content authors do the same with paired instances of the asterisk character, '*'. When a content author writes the text

  Move it from *Receiving* to *Accounts Payable*.

it will end up on the website as

  Move it from <b>Receiving</b> to <b>Accounts Payable</b>.

Given a String, text, containing zero or more usages of the italic and boldface shortcuts, translate it into HTML as demonstrated by the examples above. There will be an even number of underscores and of asterisks in text, respectively, and the spans of text enclosed by successive pairs of these characters will not overlap. To render a span of text in italics in HTML, you must start with the <i> tag and end with the </i> tag. For boldface text, start with <b> and end with </b>. After rendering all shortcuts in HTML, return the resulting text as a String.



Method signature:String expand(String text)
(be sure your method is public)


-text is between 1 and 50 characters long, inclusive
-the only characters allowed in text are the alphabetic characters 'a' to 'z' and 'A' to 'Z', the underscore '_', the asterisk '*', the space character, and the punctuation symbols ',', ';', '.', '!', '?', '-', '(', and ')'.
-the underscore '_' occurs in text an even number of times
-the asterisk '*' occurs in text an even number of times
-no substring of text enclosed by a balanced pair of underscores or by a balanced pair of asterisks may contain any further underscores or asterisks


"You _should_ see the new walrus at the zoo!"
Returns: "You <i>should</i> see the new walrus at the zoo!"
A walrus is a large, blubbery cousin of the seal.
"Move it from *Accounts Payable* to *Receiving*."
Returns: "Move it from <b>Accounts Payable</b> to <b>Receiving</b>."
Notice that a boldface span may enclose several words.
"I saw _Chelydra serpentina_ in *Centennial Park*."
Returns: "I saw <i>Chelydra serpentina</i> in <b>Centennial Park</b>."
One piece of text may include italics as well as boldface.
" _ _ __  _ yabba dabba _   *  dooooo  * ****"
Returns: " <i> </i> <i></i>  <i> yabba dabba </i>   <b>  dooooo  </b> <b></b><b></b>"
Shortcuts may enclose spaces or nothing at all.

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories:

String Manipulation