Justified Text - InterviewBit Solution

Problem: Justified Text

Problem Description:

Given a string A representing JSON object. Return an array of the string denoting JSON object with proper indentation.

Rules for proper indentation:

  • Every inner brace should increase one indentation to the following lines.

  • Every close brace should decrease one indentation to the same line and the following lines.

  • The indents can be increased with an additional ‘\t’


  1. [] and {} are only acceptable braces in this case.

  2. Assume for this problem that space characters can be done away with.

Input Format

The only argument given is the integer array A. 

Output Format

Return a list of strings, where each entry corresponds to a single line. The strings should not have "\n" character in them. 

For Example

Input 1:
    A = "{A:"B",C:{D:"E",F:{G:"H",I:"J"}}}"
Output 1:
Input 2:
    A = ["foo", {"bar":["baz",null,1.0,2]}]
Output 2:

Solution Approach:

The solution might seem lengthy but, it is quite straightforward, as you just have to follow the instructions and you will reach to your result.

Time Complexity: O(N)

Space Complexity: O(N)


Code in C++:

Note: Help us improve this blog

If you have a better solution, and you think you can help your peers to understand this problem better, then please drop your solution and approach in the comments section below.

We will upload your approach and solution here by giving you the proper credit so that you can showcase it among your peers.