HTML Elements Single-Tag Correction
Learn this problemProblem statement
Given a string str containing plain text and HTML-like tags, determine whether its tags are properly nested. The only tag names used are b, i, em, div, and p.
A string is properly nested when every closing tag matches the most recently opened tag that has not yet been closed.
If str is properly nested, return the string true. Otherwise, exactly one tag name can be changed to a different allowed tag name to make the string properly nested. The tag remains opening or closing after the change. Return the original name of the earliest tag token whose name can be changed to make the whole string properly nested.
Function
htmlElements(str: String) → StringExamples
Example 1
str = "<div><b><p>hello world</p></b></div>"return = "true"Each closing tag matches the most recently opened tag, so the string is already properly nested.
Example 2
str = "<div><i>hello</i>world</b>"return = "div"Changing the first <div> tag to <b> makes the string properly nested, so return its original name, div.
Example 3
str = "<div><div><b></b></div></p>"return = "div"Changing the first <div> tag to <p> makes all tags match. Although changing the final closing tag also works, the first tag occurs earlier.
Example 4
str = "<div>abc</div><p><em><i>test test test</b></em></p>"return = "i"Changing <i> to <b> makes the second top-level element properly nested.
Constraints
- Every tag name is one of
b,i,em,div, orp. - If
stris not already properly nested, changing exactly one tag name can make it properly nested.