{"id":"60e0e7c4-cb59-4418-85b2-274bf7aab128","name":"Mini Parser","description":"Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return the deserialized NestedInteger.\r\nEach element is either an integer or a list whose elements may also be integers or other lists.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= s.length &lt;= 5 * 104\r\n2. s consists of digits, square brackets \"[]\", negative sign '-', and commas ','.\r\n3. s is the serialization of valid NestedInteger.","sampleCode":{"cpp":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static NestedInteger deserialize(String s) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n NestedInteger result = deserialize(read.readLine());\r\n Stack<String> st = new Stack<>();\r\n display(result, st);\r\n PrintWriter out = new PrintWriter(System.in);\r\n while (st.size() > 0) {\r\n out.println(st.pop());\r\n }\r\n out.close();\r\n }\r\n\r\n public static void display(NestedInteger ni, Stack<String> st) {\r\n if (ni.isInteger()) {\r\n st.push(\"\" + ni.getInteger());\r\n } else {\r\n for (var v : ni.getList()) {\r\n st.push(\"[\");\r\n display(v, st);\r\n st.push(\"]\");\r\n }\r\n }\r\n }\r\n\r\n public static class NestedInteger {\r\n // Constructor initializes an empty nested list.\r\n final boolean isInt;\r\n final int data;\r\n final ArrayList<NestedInteger> al = new ArrayList<>();\r\n public NestedInteger() {\r\n isInt = false;\r\n data = 0;\r\n };\r\n // Constructor initializes a single integer.\r\n public NestedInteger(int value) {\r\n isInt = true;\r\n data = value;\r\n };\r\n // @return true if this NestedInteger holds a single integer, rather than a nested list.\r\n public boolean isInteger() {\r\n return isInt;\r\n }\r\n // @return the single integer that this NestedInteger holds, if it holds a single integer\r\n // Return null if this NestedInteger holds a nested list\r\n public Integer getInteger() {\r\n return data;\r\n }\r\n // Set this NestedInteger to hold a nested list and adds a nested integer to it.\r\n public void add(NestedInteger ni) {\r\n al.add(ni);\r\n }\r\n // @return the nested list that this NestedInteger holds, if it holds a nested list\r\n // Return empty list if this NestedInteger holds a single integer\r\n public List<NestedInteger> getList() {\r\n return al;\r\n }\r\n }\r\n\r\n}\r\n"},"java":{"code":""},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"[123,[456,[789]]]","sampleOutput":"]\r\n]\r\n]\r\n789\r\n[\r\n[\r\n]\r\n456\r\n[\r\n[\r\n]\r\n123\r\n[\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"206c0b6b-64c9-45cd-812f-ed43217328a8","name":"Mini Parser","slug":"mini-parser","type":1}],"next":{"id":"4949a3e2-91ff-4e15-9e44-b2f47a0a549d","name":"Mini Parser MCQ","type":0,"slug":"mini-parser-mcq"},"prev":{"id":"a05e23d3-9842-415a-8e17-59bb82e83580","name":"Baseball Game MCQ","type":0,"slug":"baseball-game-mcq"}}}

Mini Parser

Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return the deserialized NestedInteger. Each element is either an integer or a list whose elements may also be integers or other lists.

{"id":"60e0e7c4-cb59-4418-85b2-274bf7aab128","name":"Mini Parser","description":"Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return the deserialized NestedInteger.\r\nEach element is either an integer or a list whose elements may also be integers or other lists.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= s.length &lt;= 5 * 104\r\n2. s consists of digits, square brackets \"[]\", negative sign '-', and commas ','.\r\n3. s is the serialization of valid NestedInteger.","sampleCode":{"cpp":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static NestedInteger deserialize(String s) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n NestedInteger result = deserialize(read.readLine());\r\n Stack<String> st = new Stack<>();\r\n display(result, st);\r\n PrintWriter out = new PrintWriter(System.in);\r\n while (st.size() > 0) {\r\n out.println(st.pop());\r\n }\r\n out.close();\r\n }\r\n\r\n public static void display(NestedInteger ni, Stack<String> st) {\r\n if (ni.isInteger()) {\r\n st.push(\"\" + ni.getInteger());\r\n } else {\r\n for (var v : ni.getList()) {\r\n st.push(\"[\");\r\n display(v, st);\r\n st.push(\"]\");\r\n }\r\n }\r\n }\r\n\r\n public static class NestedInteger {\r\n // Constructor initializes an empty nested list.\r\n final boolean isInt;\r\n final int data;\r\n final ArrayList<NestedInteger> al = new ArrayList<>();\r\n public NestedInteger() {\r\n isInt = false;\r\n data = 0;\r\n };\r\n // Constructor initializes a single integer.\r\n public NestedInteger(int value) {\r\n isInt = true;\r\n data = value;\r\n };\r\n // @return true if this NestedInteger holds a single integer, rather than a nested list.\r\n public boolean isInteger() {\r\n return isInt;\r\n }\r\n // @return the single integer that this NestedInteger holds, if it holds a single integer\r\n // Return null if this NestedInteger holds a nested list\r\n public Integer getInteger() {\r\n return data;\r\n }\r\n // Set this NestedInteger to hold a nested list and adds a nested integer to it.\r\n public void add(NestedInteger ni) {\r\n al.add(ni);\r\n }\r\n // @return the nested list that this NestedInteger holds, if it holds a nested list\r\n // Return empty list if this NestedInteger holds a single integer\r\n public List<NestedInteger> getList() {\r\n return al;\r\n }\r\n }\r\n\r\n}\r\n"},"java":{"code":""},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"[123,[456,[789]]]","sampleOutput":"]\r\n]\r\n]\r\n789\r\n[\r\n[\r\n]\r\n456\r\n[\r\n[\r\n]\r\n123\r\n[\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"206c0b6b-64c9-45cd-812f-ed43217328a8","name":"Mini Parser","slug":"mini-parser","type":1}],"next":{"id":"4949a3e2-91ff-4e15-9e44-b2f47a0a549d","name":"Mini Parser MCQ","type":0,"slug":"mini-parser-mcq"},"prev":{"id":"a05e23d3-9842-415a-8e17-59bb82e83580","name":"Baseball Game MCQ","type":0,"slug":"baseball-game-mcq"}}}
plane

Editor


Loading...

Mini Parser

medium

Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return the deserialized NestedInteger. Each element is either an integer or a list whose elements may also be integers or other lists.

Constraints

1. 1 <= s.length <= 5 * 104 2. s consists of digits, square brackets "[]", negative sign '-', and commas ','. 3. s is the serialization of valid NestedInteger.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

[123,[456,[789]]]

Sample Output

] ] ] 789 [ [ ] 456 [ [ ] 123 [

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode