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,938 questions

51,875 answers

573 users

How to generate all possible permutations of a string in Java

3 Answers

0 votes
public class MyClass {
    public static void print_permutations(String s) { 
        print_permutations("", s); 
    }
    
    private static void print_permutations(String permutation, String s) {
        int len = s.length();
        
        if (len == 0) {
            System.out.println(permutation);
        }
        else {
            for (int i = 0; i < len; i++) {
                print_permutations(permutation + s.charAt(i), s.substring(0, i) + s.substring(i + 1, len));
            }
        }
    }
    
    public static void main(String args[]) {
        print_permutations("abcd");
    }
}

   
   
   
/*
run:
   
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba
   
*/

 



answered Feb 17, 2021 by avibootz
edited Nov 5, 2023 by avibootz
0 votes
public class MyClass {
    static void print_permutations(String s, String permutation) { 
        int len = s.length();
        if (len == 0) { 
            System.out.println(permutation + " "); 
            return; 
        } 
  
        for (int i = 0; i < len; i++) { 
            char ch = s.charAt(i); 

            print_permutations(s.substring(0, i) + s.substring(i + 1), permutation + ch); 
        } 
    } 
    public static void main(String args[]) {
        String s = "abcd"; 
        
        print_permutations(s, ""); 
    }
}




/*
run:

abcd 
abdc 
acbd 
acdb 
adbc 
adcb 
bacd 
badc 
bcad 
bcda 
bdac 
bdca 
cabd 
cadb 
cbad 
cbda 
cdab 
cdba 
dabc 
dacb 
dbac 
dbca 
dcab 
dcba 

*/

 



answered Feb 17, 2021 by avibootz
0 votes
import java.util.ArrayList;
import java.util.List;
  
public class MyClass {
    public static List<String> generatePermutations(String str) {
        List<String> permutations = new ArrayList<>();
        
        if (str.length() == 0) {
            permutations.add("");
            
            return permutations;
        }
        
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            String substring = str.substring(0, i) + str.substring(i + 1);
            List<String> subpermutations = generatePermutations(substring);
            for (String sp : subpermutations) {
                permutations.add(ch + sp);
            }
        }
        
        return permutations;
    }
    public static void main(String[] args) {
        String str = "abcd";
        
        List<String> permutations = generatePermutations(str);
        
        for (String s : permutations) {
            System.out.println(s);
        }
    }
}
   
   
   
/*
run:
   
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba
   
*/

 



answered Nov 5, 2023 by avibootz
...