1
    public List<String> generateParenthesis(int n) {
        List<String> list = new ArrayList<>();
        backtrack(list,"",0,0,3); 
        return list;
    }
    
    public void backtrack(List<String> list, String str,int open, int close, int max) {
        
        if(str.length() == max*2) {
            list.add(str);
            return;
        }
        
        if(open < max)
            backtrack(list, str+"(", open+1, close, max);
        if(close < open)
            backtrack(list, str+")", open, close+1, max);
    }
}

Can anyone explain the backtracking logic. I tried debugging but was not able to fully understand the concept.

Nigel
  • 95
  • 1
  • 10

0 Answers0