Answers for "Generate Paranthesis"

0

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;
         }
        }
       }
Posted by: Guest on June-23-2021

Browse Popular Code Answers by Language