Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to print all possible ways to break a string in brackets with Java

1 Answer

0 votes
public class MyClass {
    private static void break_string_in_bracket(String str, int index, String form) {
    	if (index == str.length()) {
    		System.out.println(form);
    	}
    
    	for (int i = index; i < str.length(); i++) {
    		String temp = form;
    		temp += "(";
    		temp += str.substring(index, i + 1);
    		temp += ")";
    		break_string_in_bracket(str, i + 1, temp);
    	}
    }

    public static void main(String args[]) {
        String str = "abcd";

	    break_string_in_bracket(str, 0, "");
    }
}





/*
run:

(a)(b)(c)(d)
(a)(b)(cd)
(a)(bc)(d)
(a)(bcd)
(ab)(c)(d)
(ab)(cd)
(abc)(d)
(abcd)

*/

 



answered Aug 26, 2023 by avibootz
...