Convert The HTML Entities

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
4 upvotes
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.”
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1 :
2
&quot;Coding Ninjas is a platform to practice coding &amp; participate in events.&quot;
&quot;Coding Ninjas is any coder&apos;s best friend.&quot;
Sample Output 1 :
“Coding Ninjas is a platform to practice coding & participate in events.”
"Coding Ninjas is any coder's best friend."
Explanation of Sample Input 1 :
In the first test case, 
The string after entity parsing will look like this:
“Coding Ninjas is a platform to practice coding & participate in events.”

In the second test case, 
The string after entity parsing will look like this:
"Coding Ninjas is any coder's best friend."  
Sample Input 2 :
2
codingninjas.com&sol;codestudio&sol;problems
Happy Coding:)
Sample Output 2 :
codingninjas.com/codestudio/problems
Happy Coding:)
Hint

Try to find HTML entities in the given string.

Approaches (1)
Brute Force

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’.
Time Complexity

O(|STR|), where |STR| is the string ‘STR’ length.

 

We are traversing through every character of the string ‘STR’. Therefore the time complexity is O(|STR|).

Space Complexity

O(|STR|), where |STR| is the length of the string ‘STR’ length

 

We are using an unordered map to store the corresponding characters of HTML entities of constant space and a string ‘ans’ to store the converted string of length ‘STR’. So the space complexity is O(|STR|).

Code Solution
(100% EXP penalty)
Convert The HTML Entities
Full screen
Console