Meta tags:
description= Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid. A code snippet is va...;
keywords= leetcode, javascript, solutions, algorithms, data structures, coding, programming, coding challenges, coding problems, coding solutions;
author= LeetCodeJavascript.com;
Headings (most frequently used words):
591, tag, validator, problem, description, solution,
Text of the page (most frequently used words):
the (23), tag (18), code (16), and (12), valid (11), tag_name (11), return (9), stack (7), const (6), false (6), cdata (6), characters (6), length (5), invalid (5), unmatched (5), closed (5), cdata_content (4), validator (4), has (4), you (4), start (4), end (4), tags (4), tag_content (4), string (3), indexof (3), else (3), contain (3), should (3), following (3), subsequent (3), all (3), otherwise (3), leetcodejavascript (2), com (2), function (2), startswith (2), slice (2), test (2), may (2), any (2), parse (2), parsed (2), format (2), range (2), find (2), when (2), not (2), necessarily (2), with (2), same (2), are (2), only (2), snippet (2), view (2), github (2), leetcode (2), 591 (2), solutions (2), 2025, josh, crozier, param, boolean, var, isvalid, for, let, pop, push, solution, forbid, even, some, that, can, matter, treat, regular, defined, between, first, cannot, until, next, exists, vice, versa, however, also, need, consider, issue, unbalanced, nested, other, see, note1, except, upper, case, letters, exactly, among, them, must, wrapped, rules, hold, given, representing, implement, whether, problem, description, hard, back, star, javascript,
Text of the page (random words):
591 tag validator leetcode javascript solutions leetcodejavascript com star on github back to all solutions 591 tag validator hard view on leetcode view on github string stack problem description given a string representing a code snippet implement a tag validator to parse the code and return whether it is valid a code snippet is valid if all the following rules hold the code must be wrapped in a valid closed tag otherwise the code is invalid a closed tag not necessarily valid has exactly the following format tag_name tag_content tag_name among them tag_name is the start tag and tag_name is the end tag the tag_name in start and end tags should be the same a closed tag is valid if and only if the tag_name and tag_content are valid a valid tag_name only contain upper case letters and has length in range 1 9 otherwise the tag_name is invalid a valid tag_content may contain other valid closed tags cdata and any characters see note1 except unmatched unmatched start and end tag and unmatched or closed tags with invalid tag_name otherwise the tag_content is invalid a start tag is unmatched if no end tag exists with the same tag_name and vice versa however you also need to consider the issue of unbalanced when tags are nested a is unmatched if you cannot find a subsequent and when you find a or all the subsequent characters until the next should be parsed as tag_name not necessarily valid the cdata has the following format cdata cdata_content the range of cdata_content is defined as the characters between cdata and the first subsequent cdata_content may contain any characters the function of cdata is to forbid the validator to parse cdata_content so even it has some characters that can be parsed as tag no matter valid or invalid you should treat it as regular characters solution param string code return boolean var isvalid function code const stack for let i 0 i code length if i 0 stack length return false if code startswith cdata i const j i 9 i code indexof j if i 1 return false i 3 else if code startswith i const j i 2 i code indexof j if i 1 return false const tag code slice j i if stack length stack pop tag a z 1 9 test tag return false i else if code i const j i 1 i code indexof j if i 1 return false const tag code slice j i if a z 1 9 test tag return false stack push tag i else i return stack length 2025 leetcodejavascript com josh crozier
|