Last Updated: 25 Apr, 2021

Convert The HTML Entities

Moderate
Asked in company
eBay

Problem statement

You are given a string “STR”, of characters and HTML entities, your task is to convert the given string’s HTML entities into their single characters and get the HTML string.

The entities used in this problem are :
" is the code used for quotation marks symbol: (“)
' is the code used for single quotation marks or apostrophe symbol: (‘)
&lt; is the code used for less than symbol: (<)
&gt; is the code used for greater than symbol: (>)
&amp; is the code used for the ampersand symbol: (&)
&sol; is the code used for the slash symbol: (/)
&bsol; is the code used for the backslash symbol: (\)
For Example :
“STR”: &quot; Coding Ninjas is a platform to practice coding &amp; participate in events. &quot;
Which on converting will look like: “Coding Ninjas is a platform to practice coding & participate in events.”
Input Format :
The first line contains an integer ‘T’, which denotes the number of test cases or queries to be run. Then the test cases are as follows.

The first and the only line of each test case contains the string 'STR'.
Output Format :
For each test case, print the converted sentence.

Print the output of each test case in a separate line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= |STR| <= 10^5

String STR contains only lowercase letters.

Where ‘|STR|’, denotes the size of the string 'STR'.

Time limit: 1 sec

Approaches

01 Approach

The basic idea is to traverse the given string and find if the given string contains HTML entities. If any entity is found, convert it to its corresponding single character.

 

Algorithm

 

  • Initialize an unordered map “Entities” that maps from string to string where the key stores the given entity and the value at any key stores its corresponding character.
  • Initialize a string ‘ans’ to store the converted string and a string ‘temp’ initialized to an empty string.
  • Iterate over the given string using the variable ‘i’.
    • If the map contains a key = temp
      • Add the value at the key ‘temp’ to ‘ans’. It denotes that if any entity is encountered, add its corresponding character to ‘ans’ and make the ‘temp’ string empty.
    • If the character at ‘i’-th position is ‘&’
      • Add ‘temp’ to ‘ans’ and make the string temp = STR[ i ].
    • Otherwise, if the character at STR[i] is ‘space’, add ‘temp’ to ‘ans’, add space to ‘ans’ and make the ‘temp’ string empty.
    • Otherwise, add character at STR[i] to ‘temp’.
  • If ‘temp’ is one of the HTML entities then add its corresponding character to ‘ans’, otherwise add ‘temp’ to ‘ans’.
  • Return ‘ans’.