Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/022_GenerateParentheses22.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory99 lines (85 loc) · 2.58 KB masterBreadcrumbsLeetcode_Java/022_GenerateParentheses22.javaCopy pathTopFile metadata and controlsCodeBlame99 lines (85 loc) · 2.58 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899class Solution { public List<String> generateParenthesis(int n) { List<String> list = new ArrayList(); traverse(0,0,n,"",list); return list; } void traverse(int op, int cp, int n, String prefix, List<String> list){ if((op+cp)/2 == n){ list.add(prefix); return; } if(op<n) traverse(op+1, cp, n, prefix+"(",list); if(cp<n && cp<op) traverse(op,cp+1,n, prefix+")",list); }} **************************************************************************************** class Solution { List<String> ans = new ArrayList<String>(); public List<String> generateParenthesis(int n) { generate(n, 0, new StringBuilder()); return ans; } public void generate(int n, int openBrackets, StringBuilder sb){ if(openBrackets == 0 && n == 0){ String temp = sb.toString(); ans.add(temp); return; } if(openBrackets != 0){ sb.append(')'); generate(n, openBrackets-1, sb); sb.setLength(sb.length() - 1); } if(n != 0){ sb.append('('); generate(n-1, openBrackets+1, sb); sb.setLength(sb.length() - 1); } }} *************************************************************************************class Solution { public List<String> generateParenthesis(int n) { int open = n ; int close = n; String output = ""; ArrayList<String> list = new ArrayList<>(); solve(open,close,output,list); return list; } public void solve(int open,int close,String output,ArrayList<String> list) { if(open == 0 && close == 0) { list.add(output); return; } if(open != 0) { String output1 = output + "(" ; solve(open-1,close,output1,list); } if(close > open) { String output2 = output + ")"; solve(open,close-1,output2,list); } }} ******************************************************************************** class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<>(); dfs(n, n, "", res); return res; } private void dfs(int l, int r, String cur, List<String> res) { if(l == 0 && r == 0){ res.add(cur); return; } if(l > 0) dfs(l-1, r, cur+"(", res); if(r > l) dfs(l, r-1, cur+")", res); }} You can’t perform that action at this time.