My java solution


  • 1
    L
    public class Solution {
    public List<String> generateParenthesis(int n) {
        Set<String> set=generate(n);
        List<String> list=new ArrayList<String>(set);
        return list;
    }
    public Set<String> generate(int n){
        if(n==1){
            Set<String> set=new HashSet<String>();
            set.add("()");
            return set;
        }
        else{
            Set<String> set=new HashSet<String>();
            Set<String> setTemp=generate(n-1);
            Iterator<String> it=setTemp.iterator();
            while(it.hasNext()){
                String value=it.next();
                for(int i=0;i<value.length()-1;i++)
                {
                        set.add(value.substring(0,i+1)+"()"+value.substring(i+1,value.length()));
                }
                set.add("()"+value);
                set.add(value+"()");
            }
            return set;
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.