HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Printing braces

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bracesprintingstackoverflow

Problem

Question goes like this :

input: 1
output:
{}

input: 2
output:
{}{}
{{}}

input: 3
output:
{}{}{}
{{}}{}
{}{{}}


This is my program :

public class PrintBraces {
    int n;
    char []braces;

    void readData()
    {
        java.util.Scanner scn=new java.util.Scanner(System.in);
        System.out.print("Please enter the value of n : ");
        n=scn.nextInt();
    }

    void manipulate()
    {
        braces=new char[n*2];

        for(int i=0;i0)
            {
                char temp=braces[oddNo];
                braces[oddNo]=braces[oddNo+1];
                braces[oddNo+1]=temp;

                print();

                temp=braces[oddNo];
                braces[oddNo]=braces[oddNo+1];
                braces[oddNo+1]=temp;
            }
            else
            {
                print();
            }
        }
    }

    void print()
    {
        for(int i=0;i<2*n;i++)
        {
            System.out.print(braces[i]);
        }
        System.out.println();
    }
}


class PrintMain
{
    public static void main(String args[])
    {
        PrintBraces pb=new PrintBraces();
        pb.readData();
        pb.manipulate();
    }
}


As expected, I get the correct answer.

I have solved it but I think it isn't efficient enough. Can anyone optimise it? And I would love to see any other alternative approaches for the same problem. May be a recursive one?
Also, I am open to any suggestions for improving my programming style. Any good practices that I may be violating in my code?

Solution

public class Solution{

    public static void par(int n, int open, int closed, String str) {

        if (closed == n) {
            System.out.println(str);
            return;
        }

        if (open < n) {
            par(n, open+1, closed, str + "{");
        }

        if (closed < open) {
            par(n, open, closed+1, str + "}");
        }
    }

    public static void main(String[] args) throws Exception {

        par(Integer.parseInt(args[0]), 0, 0, "");

    }
}


Output fot the testcase 3:

{{{}}}
{{}{}}
{{}}{}
{}{{}}
{}{}{}


If you need additional speed - you should use the buffered output. Something like this:

import java.io.*;
import java.util.*;

public class Solution{

    static BufferedWriter out; 

    public static void par(int n, int open, int closed, String str)  throws IOException {

        if (closed == n) {
            out.write(str + "\n");
            return;
        }

        if (open < n) {
            par(n, open+1, closed, str + "{");
        }

        if (closed < open) {
            par(n, open, closed+1, str + "}");
        }
    }

    public static void main(String[] args) throws IOException  {
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 4096);
        par(Integer.parseInt(args[0]), 0, 0, "");
        out.flush();
    }
}


For example the first version for 13 takes ~ 17 seconds:

timethis %JAVA_HOME%/bin/java -server Solution 13 >13.txt


the buffered output version ~ 0.4 second!

PS. Interesting that jdk1.7.0_04 ~ 5 times faster than that c version:

#include
#include

unsigned long pairs_count;
unsigned long *brackets;
unsigned long found = 0;

void print_result()
{
    unsigned long i, j;

    for (i = 0; i  pos)
    k = start_val;
    else
    k = pos;

    for (i = k; i < pairs_count; i++) {
    brackets[pos] = i;

    if (pos == pairs_count - 1)
        print_result();
    else
        step(i, pos + 1);
    }
}

int main()
{

    scanf("%d", &pairs_count);

    brackets = new unsigned long[pairs_count];

    if (!brackets) {
    printf("Unsufficient memory.n");
    return (0);
    }

    step(0, 0);

    delete brackets;

    printf("Found: %d.n", found);

    return (0);
}

Code Snippets

public class Solution{

    public static void par(int n, int open, int closed, String str) {

        if (closed == n) {
            System.out.println(str);
            return;
        }

        if (open < n) {
            par(n, open+1, closed, str + "{");
        }

        if (closed < open) {
            par(n, open, closed+1, str + "}");
        }
    }

    public static void main(String[] args) throws Exception {

        par(Integer.parseInt(args[0]), 0, 0, "");

    }
}
{{{}}}
{{}{}}
{{}}{}
{}{{}}
{}{}{}
import java.io.*;
import java.util.*;

public class Solution{

    static BufferedWriter out; 

    public static void par(int n, int open, int closed, String str)  throws IOException {

        if (closed == n) {
            out.write(str + "\n");
            return;
        }

        if (open < n) {
            par(n, open+1, closed, str + "{");
        }

        if (closed < open) {
            par(n, open, closed+1, str + "}");
        }
    }

    public static void main(String[] args) throws IOException  {
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 4096);
        par(Integer.parseInt(args[0]), 0, 0, "");
        out.flush();
    }
}
timethis %JAVA_HOME%/bin/java -server Solution 13 >13.txt
#include<stdio.h>
#include<string.h>

unsigned long pairs_count;
unsigned long *brackets;
unsigned long found = 0;

void print_result()
{
    unsigned long i, j;

    for (i = 0; i < pairs_count; i++) {
    printf("(");

    for (j = 0; j < pairs_count; j++)
        if (brackets[j] == i)
        printf(")");
    }

    printf("n");
    found++;
}

void step(unsigned long start_val, unsigned long pos)
{
    unsigned long k, i;

    if (start_val > pos)
    k = start_val;
    else
    k = pos;

    for (i = k; i < pairs_count; i++) {
    brackets[pos] = i;

    if (pos == pairs_count - 1)
        print_result();
    else
        step(i, pos + 1);
    }
}

int main()
{

    scanf("%d", &pairs_count);

    brackets = new unsigned long[pairs_count];

    if (!brackets) {
    printf("Unsufficient memory.n");
    return (0);
    }

    step(0, 0);

    delete brackets;

    printf("Found: %d.n", found);

    return (0);
}

Context

StackExchange Code Review Q#12777, answer score: 6

Revisions (0)

No revisions yet.