591. Tag Validator
Given a string representing a code snippet, you need to 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, theTAG_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, theTAG_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 ofCDATA_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 parseCDATA_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.
Valid Code Examples:
Input: "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
Output: True
Explanation:
The code is wrapped in a closed tag : <DIV> and </DIV>.
The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata.
Although CDATA_CONTENT has unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as tag.
So TAG_CONTENT is valid, and then the code is valid. Thus return true. Input: "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
Output: True
Explanation:
We first separate the code into : start_tag|tag_content|end_tag.
start_tag -> "<DIV>"
end_tag -> "</DIV>"
tag_content could also be separated into : text1|cdata|text2.
text1 -> ">> ![cdata[]] "
cdata -> "<![CDATA[<div>]>]]>", where the CDATA_CONTENT is "<div>]>"
text2 -> "]]>>]" The reason why start_tag is NOT "<DIV>>>" is because of the rule 6.
The reason why cdata is NOT "<![CDATA[<div>]>]]>]]>" is because of the rule 7.Invalid Code Examples:
Input: "<A> <B> </A> </B>"
Output: False
Explanation: Unbalanced. If "<A>" is closed, then "<B>" must be unmatched, and vice versa. Input: "<DIV> div tag is not closed <DIV>"
Output: False Input: "<DIV> unmatched < </DIV>"
Output: False Input: "<DIV> closed tags with invalid tag name <b>123</b> </DIV>"
Output: False Input: "<DIV> unmatched tags with invalid tag name </1234567890> and <CDATA[[]]> </DIV>"
Output: False Input: "<DIV> unmatched start tag <B> and unmatched end tag </C> </DIV>"
Output: False
Note:
- For simplicity, you could assume the input code (including the any characters mentioned above) only contain
letters
,digits
,'<'
,'>'
,'/'
,'!'
,'['
,']'
and' '
.
Approach #1: Simulate. [Java]
class Solution {
public boolean isValid(String code) {
Stack<String> stack = new Stack<>();
int i, j;
for (i = 0; i < code.length(); ) {
if (i > 0 && stack.empty()) return false;
if (code.startsWith("<![CDATA[", i)) {
j = i + 9;
i = code.indexOf("]]>", j);
if (i < 0) return false;
i += 3;
} else if (code.startsWith("</", i)) {
j = i + 2;
i = code.indexOf(">", j);
if (i < 0 || i - j > 9 || i == j) return false;
for (int k = j; k < i; ++k)
if (!Character.isUpperCase(code.charAt(k))) return false;
String substr = code.substring(j, i++);
if (stack.empty() || !stack.pop().equals(substr)) return false;
} else if (code.startsWith("<", i)) {
j = i + 1;
i = code.indexOf(">", j);
if (i < 0 || i - j > 9 || i == j) return false;
for (int k = j; k < i; ++k)
if (!Character.isUpperCase(code.charAt(k))) return false;
String substr = code.substring(j, i++);
stack.push(substr);
} else {
i++;
}
}
return stack.empty();
}
}
Reference:
591. Tag Validator的更多相关文章
- [LeetCode] Tag Validator 标签验证器
Given a string representing a code snippet, you need to implement a tag validator to parse the code ...
- [Swift]LeetCode591. 标签验证器 | Tag Validator
Given a string representing a code snippet, you need to implement a tag validator to parse the code ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)
All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...
- leetcode 学习心得 (3)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- [LeetCode] Add Bold Tag in String 字符串中增添加粗标签
Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ...
随机推荐
- 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器
update明显更新就一行,但是结果显示更新多行. 原因是有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器
- RestExpress response中addHeader 导致stackOverflow
问题描述: 最近在项目使用中要在restExpress的header中增加一个键值对,同事在使用的时候没有对header的value进行非空判断,于是在测试环境测试的时候就出现了一个异常
- python爬虫之scrapy
架构概览 本文档介绍了Scrapy架构及其组件之间的交互. 概述 接下来的图表展现了Scrapy的架构,包括组件及在系统中发生的数据流的概览(绿色箭头所示). 下面对每个组件都做了简单介绍,并给出了详 ...
- Springboot 热部署问题。亲测可用。
本人开发使用的是Mac系统,windows应该类似.主要是 spring-boot-devtools的使用 参考:mac下的idea设置,是不是有疑问,既然别人写好了,为啥你还要再来一次,因为我使用了 ...
- cleos
[cleos] 1.在.bashrc中加入以下代码,方便直接使用 cleos,7777是nodeos端口,5555是keosd端口. alias cleos='docker exec -it eosi ...
- 迭代器模块 itertools
无限迭代器 itertools 包自带了三个可以无限迭代的迭代器.这意味着,当你使用他们时,你要知道你需要的到底是最终会停止的迭代器,还是需要无限地迭代下去. 这些无限迭代器在生成数字或者在长度未知的 ...
- CSS深入理解之z-index
(http://www.imooc.com/learn/643) 一.z-index基础知识 1.z-index的含义 z-index属性指定了元素及其子元素的[z顺序],而[z顺序]可以决定当元 ...
- (转)android import library switch语句报错case expressions must be constant expressions
今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions : 解决方 ...
- git的一些常用操作
一.克隆默认分支 git clone 远程地址 二.克隆某一分支 git clone -b 分支名 远程地址 三.切换分支 git branch 分支名 四.一般开发提交的流程 1).查看本地状态 g ...
- CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”
执行yum命令时出现以上错误; 解决方法: vi /etc/sysconfig/network-scripts/ifcfg-eth0 这一段为你的网卡修改图中框框部分 然后重启 :reboot