Friday, January 10, 2025
Google search engine
HomeData Modelling & AIFind the missing end tag in the given HTML Code

Find the missing end tag in the given HTML Code

Given a string htmlCode which is HTML code of a webpage, the task is to find the missing end tag in the HTML code.
Examples: 
 

Input: htmlCode = "<!DOCTYPE html>
<html>
<head>
    <title>
        neveropen
    </title>
</head>
<body>
    <button>
</body>
</html>"
Output: </button>

Input: htmlCode = "<!DOCTYPE html>
<html>
<body>
    <p>Hello</p>
    
</html>"
Output: </body>

 

Approach: The idea is to use stack to keep track of the current start tags in the HTML Code and if at any moment there is an end tag which doesn’t match to the top of the stack which denotes a start tag. Then the tag which is at the top of the stack is to be closed first. Therefore, top of the stack will be the desired missing tag in the HTML Code.
Below is the implementation of the above approach:
 

Python




# Python implementation to find the
# the missing end in the HTML code
 
# Function to Auto complete the
# missing tag in the html Code
def autoComplete(s):
     
    # Split the html Code in line
    linesOfCode = list(s.strip().split("\n"))
     
    # Tags which are self closed doesn't
    # needs to be closed
    selfClosedTags = ["area""base", "br", \
            "col",   "embed""hr",    "img", \
            "input", "link", "meta", "param", \
                    "source", "track", "wbr"]
    n = len(linesOfCode)
 
    stack = []
     
    # Loop to iterate over the
    # lines of code
    for i in range(n):
        j = 0
         
        # Current Line
        line = linesOfCode[i]
        while j < len(linesOfCode[i]):
             
            # Condition to check if the current
            # character is a end tag in the code
            if j + 1 < len(line) and line[j] == "<"\
                            and line[j + 1] == "/":
                tag = []
                j += 2
                 
                # Loop to get the tag
                while j < len(line) and\
                   "a" <= line[j] <= "z":
                    tag.append(line[j])
                    j += 1
                while j < len(line) and line[j] != ">":
                    j += 1
                if stack[-1] != tag:
                    tag = stack[-1]
                    return "</" + "".join(tag) + ">"
                stack.pop()
                 
            # Condition to check if the current
            # character denotes the code is
            # of the HTML 5
            elif j + 1 < len(line) and line[j] == "<"\
                                  and line[j] == "!":
                continue
                 
            # Condition to check if the current
            # tag is a start tag of the HTML Code
            elif line[j] == "<":
                tag = []
                j += 1
                 
                # Loop to get the tag of the HTML
                while j < len(line) and\
                      "a" <= line[j] <= "z":
                    tag.append(line[j])
                    j += 1
                while j < len(line) and line[j] != ">":
                    j += 1
                     
                # Condition to check if the
                # current tag is not a self closed tag
                if "".join(tag) not in selfClosedTags:
                    stack.append(tag)
            j += 1
             
    # Condition to check if any tag
    # is unbalanced then return that tag
    if stack:
        tag = stack.pop()
        return "</" + "".join(tag) + ">"
    return -1
 
# Driver Code
if __name__ == "__main__":
    s = """<! DOCTYPE html>
<html>
<head>
    <title>
        neveropen
    </title>
</head>
<body>
    <button>
</body>
</html>"""
    tag = autoComplete(s)
    print(tag)


Output: 

</button>

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments