Question: FInd the time and space complexity of the following code Class solution { Public void validifblocks(int n) { If (n 0) { For (int i

FInd the time and space complexity of the following code

Class solution {

Public void validifblocks(int n) {

If (n <=0) {

Return;

}

List blocks = new Arrayslist();

helper(blocks, n, n);

}

Private void helper(list blocks, int left, int right) {

If (left ==0 && right == 0) {

print(blocks);

Return;

}

StringBuilder builder = new StringBuilder();

If (left > 0) {

For (int i = 0; i < right - left; i++) {

builder.append ( );

}

blocks.add(builder.append(if {).toString());

helper(blocks, left -1, right);

blocks.remove(blocks.size() - 1);

}

builder.setlength(0);

If (right > left ) {

For (int i = 0; i < right -left -1; i++) {

builder.append( );

}

blocks.add(builder.append(}).toString());

helper(blocks, left, right -1);

blocks.remove(blocks.size() - 1);

}

}

Private void print(list blocks) {

For (String s : blocks) {

System.out.println(s);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!