Codility---Brackets
A string S consisting of N characters is considered to be properly nestedif any of the following conditions is true:
- S is empty;
- S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
- S has the form "VW" where V and W are properly nested strings.
For example, the string "{[()()]}" is properly nested but "([)()]" is not.
Write a function:
class Solution { public int solution(String S); }
that, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise.
For example, given S = "{[()()]}", the function should return 1 and given S = "([)()]", the function should return 0, as explained above.
Assume that:
- N is an integer within the range [0..200,000];
- string S consists only of the following characters: "(", "{", "[", "]", "}" and/or ")".
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Stack;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if (S.length() % 2 != 0) {
return 0;
}
Character openingBrace = new Character('{');
Character openingBracket = new Character('[');
Character openingParen = new Character('(');
Stack<Character> openingStack = new Stack<Character>();
for (int i = 0; i < S.length(); i++) {
char c = S.charAt(i);
if (c == openingBrace || c == openingBracket || c == openingParen) {
openingStack.push(c);
} else {
if (i == S.length()-1 && openingStack.size() != 1) {
return 0;
}
if (openingStack.isEmpty()) {
return 0;
}
Character openingCharacter = openingStack.pop();
switch (c) {
case '}':
if (!openingCharacter.equals(openingBrace)) {
return 0;
}
break;
case ']':
if (!openingCharacter.equals(openingBracket)) {
return 0;
}
break;
case ')':
if (!openingCharacter.equals(openingParen)) {
return 0;
}
break;
default:
break;
}
}
}
if (! openingStack.isEmpty()) {
return 0;
}
return 1;
}
}
https://codility.com/demo/results/training87ME5J-MVG/
Codility---Brackets的更多相关文章
- Brackets
按下Ctrl + E("编辑")或退出编辑.Brackets将搜索项目下所有CSS文件 Ctrl/Cmd + Alt + P 打开即时预览功能 alt + command + O目 ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Brackets前端开发IDE工具
Brackets是一个开源的前端开发IDE工具,网页设计师和前端开发人员必备的前端开发IDE工具. 它能够使你在开发WEB网站实时预览你的网页,目前版本只适用于Chrome浏览器可以实时预览效果 支持 ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- POJ 题目1141 Brackets Sequence(区间DP记录路径)
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27793 Accepted: 788 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- 前端开发利器-Brackets IDE
是什么? http://brackets.io/ A modern, open source text editor that understands web design. 现代, 开源的文本编辑器 ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
随机推荐
- idhttp post 出现exception class EIdSocketError with message 'Socket Error # 10054的解决办法(捕捉异常,防止程序挂掉)
由于项目需要.需要向一个地址提交数据.提交的次数很频繁,但是程序经常raised exception class EIdSocketError with message 'Socket Error # ...
- Javascript 基准测试
http://bubkoo.com/2014/02/18/bulletproof-javascript-benchmarks/
- 在嵌入式程序中QT去掉鼠标指针
在像arm的QT编程当中,一般都是使用触摸来操作,当是我们运行程序的时候会发现总是有个鼠标箭头在那里,下面介绍种方法将其给去掉.这样就漂亮多了.在main()函数加入 #include <QWS ...
- 网络故障模拟,cpu高压以及docker中的实现
利用tc进行丢包 通过网络丢包来模拟网络故障,是测试中一个重要的测试项目.这对服务来说可以测试其在网络故障时的异常处理的能力,对于服务的可靠性是一个相当严苛的测试. 网卡名为$netcard,丢包率为 ...
- redis在windows10上跑起来
原文:redis在windows10上跑起来 今天,开始学习redis,发现大多数redis都是在Linux上面运行的,可是我想把它放到windows上面运行,经过查找资料,在GitHub上面发现了一 ...
- 用游戏杆控制WPF中三维模型
原文:用游戏杆控制WPF中三维模型 用游戏杆控制WPF中三维模型 今天心情比较好,不写WF的文章了,换个主题.写一个我最最最擅长的内容. 例子下载: http://files.cnblogs. ...
- Nginx http filter异常排查
问题: 访问异常 root@cloud:/usr/local/nginx# curl -i http://localhost/test.html curl: (52) Empty reply from ...
- 用Nginx实现微信小程序本地SSL请求
我们在开发小程序的时候,需要填写一个授权域名.还要有一个证书.这些都是花钱的.开发非常不方便.本文主要讲解配置ssl和本地开发 小程序后台添加授权域名 这个域名你要写一个是备案过的.具体是不是你的,都 ...
- 2-16 mysql主从复制
1. 部署MYSQL主从同步 <M-S> 环境:mysql版本一致,均为5.7.18 master xuegod4 ip 192.168.10.34 数据库密码 yourpassw ...
- jquery 点谁谁哭-隐式迭代
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...