Task description

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).
 
 
Solution

 
Programming language used: Java
Total time used: 14 minutes
Code: 16:26:24 UTC, java, final, score:  100
  1. // you can also use imports, for example:
  2. // import java.util.*;
  3. // you can write to stdout for debugging purposes, e.g.
  4. // System.out.println("this is a debug message");
  5. import java.util.Stack;
  6. class Solution {
  7. public int solution(String S) {
  8. // write your code in Java SE 8
  9. if (S.length() % 2 != 0) {
  10. return 0;
  11. }
  12. Character openingBrace = new Character('{');
  13. Character openingBracket = new Character('[');
  14. Character openingParen = new Character('(');
  15. Stack<Character> openingStack = new Stack<Character>();
  16. for (int i = 0; i < S.length(); i++) {
  17. char c = S.charAt(i);
  18. if (c == openingBrace || c == openingBracket || c == openingParen) {
  19. openingStack.push(c);
  20. } else {
  21. if (i == S.length()-1 && openingStack.size() != 1) {
  22. return 0;
  23. }
  24. if (openingStack.isEmpty()) {
  25. return 0;
  26. }
  27. Character openingCharacter = openingStack.pop();
  28. switch (c) {
  29. case '}':
  30. if (!openingCharacter.equals(openingBrace)) {
  31. return 0;
  32. }
  33. break;
  34. case ']':
  35. if (!openingCharacter.equals(openingBracket)) {
  36. return 0;
  37. }
  38. break;
  39. case ')':
  40. if (!openingCharacter.equals(openingParen)) {
  41. return 0;
  42. }
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. }
  49. if (! openingStack.isEmpty()) {
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. }
  55.  
  56. https://codility.com/demo/results/training87ME5J-MVG/

Codility---Brackets的更多相关文章

  1. Brackets

    按下Ctrl + E("编辑")或退出编辑.Brackets将搜索项目下所有CSS文件 Ctrl/Cmd + Alt + P 打开即时预览功能 alt + command + O目 ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. Brackets前端开发IDE工具

    Brackets是一个开源的前端开发IDE工具,网页设计师和前端开发人员必备的前端开发IDE工具. 它能够使你在开发WEB网站实时预览你的网页,目前版本只适用于Chrome浏览器可以实时预览效果 支持 ...

  6. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  7. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  8. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  9. 前端开发利器-Brackets IDE

    是什么? http://brackets.io/ A modern, open source text editor that understands web design. 现代, 开源的文本编辑器 ...

  10. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

随机推荐

  1. express笔记

    1.req.query: 获取get请求的查询字符串对象 2.req.body: 获取post请求的查询字符串对象,要使用该方法需要先使用body-parser中间件,app.use(bodyPars ...

  2. JTextpane 加入的行号

    最近项目需求,在需求JTextPane加入行号等信息,网上找了半天才发现JTextArea加入行号信息.copy正在研究在线程序.他发现自己能够做出改变来改变JTextPane显示行号. 代码: pa ...

  3. 关于MOVE 和 CopyMemory 的用法区别

    最近做了一个数据采集服务器, 根据程序的框架,使用了大量的指针结构体(内存块) 操作. 例子: PArrayByte = ^TArrayByte;  TArrayByte = packed recor ...

  4. wpf控件设计时支持(2)

    原文:wpf控件设计时支持(2) 这篇介绍在wpf设计时集合项属性添加项的定义和自定义控件右键菜单的方法 集合项属性设计时支持 1.为集合属性设计器识别具体项类型 wpf设计器允许定义集合项的类型,如 ...

  5. REST = HTTP动词(GET POST PUT DELETE)操作 + 服务器暴露资源URI,最后返回状态码(充分利用HTTP自身的特征,而不仅仅是把HTTP当作传输协议。Rest协议是面向资源的,SOAP是面向服务的),表现形式可以是JSON XML BIN,举例很清楚

    好处是,操作系统或者浏览器,可以重复利用它们内置的缓存机制等等. 增删改查都是一个地址,具体靠http头部信息判断. 利用HTTP协议语义构建的语义化.可缓存的接口. URL定位资源,用HTTP动词( ...

  6. C#获取windows 10的下载文件夹路径

    Windows没有为“下载”文件夹定义CSIDL,并且通过Environment.SpecialFolder枚举无法使用它. 但是,新的Vista 知名文件夹 API确实使用ID定义它FOLDERID ...

  7. broadAnywhere:Broadcast组件权限绕过漏洞(Bug: 17356824)

    原创内容,转载请注明出处 http://retme.net/index.php/2014/11/14/broadAnywhere-bug-17356824.html Lolipop源代码已经放出有些日 ...

  8. Oracle实践--PL/SQL综合之分页存储过程

    Oracle PL/SQL分页的存储过程 Oracle,分页,存储过程三个词结合起来,来个综合点的小练习,运用之前的PL/SQL创建一个分页的存储过程,仅仅须要简单几步就可以. 1.声明一个引用游标 ...

  9. Fiddler教程(Web调试工具)

    转载地址:写得很不错的fildder教程   http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站:  www.fiddler2.c ...

  10. Qt on Android 资源文件系统qrc与assets

    使用 Qt 为 Android 开发应用时,有时我们的应用会携带一些资源文件,如 png . jpg 等,也可能有一些配置文件,如 xml 等,这些文件放在哪里呢?有两种方式:qrc和assets,咱 ...