HTML Elements Single-Tag Correction
Updated
Problem 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.
More Odoo interview questions
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
- Decode StringAbridge · OAAbridge interview questions
- Nested Recipe Blocks (Game Adaptation)Roblox · OARoblox interview questions
- Minimum Remaining LengthWells Fargo · OA · PHONE SCREENWells Fargo interview questions
- Modify the StringYahoo · OAYahoo interview questions
- Maximum Score in Balanced String 🍇Amazon · OA
- Lexicographically Minimum Stack EncryptionRippling · OARippling interview questions
- Shortest String After Erasing Equal PairsMarshall Wace · OAMarshall Wace interview questions
- Create New VersionCisco · OACisco interview questions