Untitled

mail@pastecode.io avatar
unknown
python
9 days ago
2.0 kB
2
Indexable
Never
def CheckDOM(strParam):
    from collections import deque

    def extract_tokens(html):
        tokens = []
        i = 0
        n = len(html)

        while i < n:
            if html[i] == '<':
                j = i
                while j < n and html[j] != '>':
                    j += 1
                if j < n:
                    tokens.append(html[i:j+1])
                    i = j + 1
                else:
                    break
            else:
                j = i
                while j < n and html[j] != '<':
                    j += 1
                if j > i:
                    tokens.append(html[i:j])
                    i = j
                else:
                    break

        return tokens

    tokens = extract_tokens(strParam)
    stack = deque()

    def normalize_tag(tag):
        return tag.replace(' ', '')

    opening_tags = set(['<b>', '<i>', '<em>', '<div>', '<p>'])
    closing_tags = set(['</b>', '</i>', '</em>', '</div>', '</p>'])

    tag_match = {
        '<b>': '</b>',
        '<i>': '</i>',
        '<em>': '</em>',
        '<div>': '</div>',
        '<p>': '</p>',
    }

    changes_needed = 0
    change_tag = ""

    for token in tokens:
        token = normalize_tag(token)
        if token in opening_tags:
            stack.append(token)
        elif token in closing_tags:
            if stack:
                last_open = stack.pop()
                if tag_match[last_open] != token:
                    changes_needed += 1
                    if changes_needed == 1:
                        change_tag = last_open.strip('<>')
                    else:
                        return False
            else:
                return False

    if stack:
        if changes_needed == 0 and len(stack) == 1:
            change_tag = stack.pop().strip('<>')
            return change_tag
        return False

    return True if changes_needed == 0 else change_tag


print(CheckDOM(input()))
Leave a Comment