Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Generate Paranthesis

     import java.util.HashSet;
     import java.util.Set;

     public class BalancedParanthesis {
       public static void main(String args[]){
         int noOfBrackets = 3;
         HashSet<String> hs=new HashSet(generate(noOfBrackets));
         System.out.println(hs);
         }
         public static HashSet<String> generate(int in)
         {
          HashSet<String> hs= new HashSet<String>();
          if(in ==1)
         {
        hs.add("()");
        return hs;
         }
        else{
           Set<String> ab=generate(in-1);
           for(String each:ab)
            {
            hs.add("("+each+")");
            hs.add("()"+each);
            hs.add(each+"()");
            }
        return hs;
         }
        }
       }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Generate #Paranthesis
ADD COMMENT
Topic
Name
1+2 =