Ctrl K

Decode Markup Escapes

Medium
In markup some characters are written as codes. We care about six of them: | Code | Character | |---|---| | &quot; | " | | &apos; | ' | | &amp; | & | | &gt; | > | | &lt; | < | | &frasl; | / | Given a string markup, replace each of these codes with its character and return the result. Everything else, including unknown codes such as &nbsp;, stays unchanged. The string is scanned left to right exactly once: characters produced by a replacement are not decoded again. For example, &amp;gt; becomes &gt;, not >.
Example 1
markup="5 &lt; 7 &amp;&amp; 7 &gt; 5" → '5 < 7 && 7 > 5'

&lt; becomes <, each &amp; becomes &, and &gt; becomes >.

Example 2
markup="&amp;lt; stays &lt;" → '&lt; stays <'

The replacement is a single pass: &amp; turns into &, and the resulting &lt; is not decoded again.

Example 3
markup="path&frasl;to &unknown; &quotx" → 'path/to &unknown; &quotx'

&frasl; is /. &unknown; is not in the table and &quotx has no semicolon, so both stay as they are.

Constraints
  • 1 ≤ len(markup) ≤ 10⁵
  • markup may contain any of the 256 ASCII characters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumNumber in Roman Numerals MediumKeypad Words MediumAnagram Buckets MediumSplit Into Dictionary Words Medium
loading Python…
Input
markup="5 &lt; 7 &amp;&amp; 7 &gt; 5"
Expected
'5 < 7 && 7 > 5'

Run checks the first 3 tests in your browser. Submit checks all 8 tests on the server, hidden ones included.