Enumeration of all combinations Java

When I was solving the problem that I wanted to get all the combinations of characters from the given character string, I looked it up and found that chakotay's blog = 612) had what I was looking for !!!

	public static void permutation(String q, String ans){
		// Base Case
		if(q.length() <= 1) {
			System.out.println(ans + q);
		}
		// General Case
		else {
			for (int i = 0; i < q.length(); i++) {
				permutation(q.substring(0, i) + q.substring(i + 1),
						ans + q.charAt(i));
			}
		}
	}

I was impressed by the very concise code, even though I searched various things including English !!! But I don't know what's going on. .. .. Therefore, I would like to look at recursion step by step. As an example, let's take a look at the behavior of permutation ("abc", "") in order.

permutation("abc","")
//q.length=3 so go to else
//i=When 0(This i is less than 3)
	permutation("bc","a")
	//q.length=2 so go to else
	//i=When 0(This i is less than 2)
		permutation("c","ab")
		//q.length=1 so go to if
		System.out.println("abc");
	//i=When 1(This i is less than 2)
		permutation("b","ac")
		//q.length=1 so go to if
		System.out.println("acb");
	//permutation("bc","a") done	
//i=When 1(This i is less than 3)
	permutation("ac","b")
	//q.length=2 so go to else
	//i=When 0(This i is less than 2)
		permutation("c","ba")
		//q.length=1 so go to if
		System.out.println("bac");
	//i=When 1(This i is less than 2)
		permutation("a","bc")
		//q.length=1 so go to if
		System.out.println("bca");
	//permutation("ac","b") done	
//i=When 2(This i is less than 3)
	permutation("ab","c")
	//q.length=2 so go to else
	//i=When 0(This i is less than 2)
		permutation("b","ca")
		//q.length=1 so go to if
		System.out.println("cab");
	//i=When 1(This i is less than 2)
		permutation("a","cb")
		//q.length=1 so go to if
		System.out.println("cba");
	//permutation("ab","c") done	
//permutation("abc","") done

It is like this. Is it difficult to understand? I wrote it down and understood it, but I often admired that I couldn't come up with such a method on my own.

that's all. It was my first post.

Recommended Posts

Enumeration of all combinations Java
[Java] Enumeration type
[Java] Overview of Java
Expired collection of java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
java --Unification of comments
History of Java annotation
java (merits of polymorphism)
[Java twigs] Enumerate combinations
NIO review of java
All about Java programming
[Java] Three features of Java
Summary of Java support 2018
About an instance of java
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②
[Java] Practice of exception handling [Exception]
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
[Java] Creation of original annotation
4th day of java learning
Java end of month plusMonths
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
Implement math combinations in Java
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
java (inheritance of is-a principle)
Implementation of gzip in java
Advantages and disadvantages of Java
Benefits of Java static method
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java] Speed comparison of string concatenation
Think of a Java update strategy
[Java] Delete the elements of List
[For beginners] Summary of java constructor
Various methods of Java String class
About fastqc of Biocontainers and Java
Summary of [Java silver study] package
About Lambda, Stream, LocalDate of Java8
Story of passing Java Gold SE8
Sort a List of Java objects
[Java] Output by FormatStyle of DateTimeFormatter
[Java] Input to stdin of Process
Basic usage of java Optional Part 1
Handling of time zones using Java
[Java EE] Common misunderstanding of @Dependent
List of members added in Java 9
[Java] Reflash all items in BeanClass
[Java] Judgment of identity and equivalence
Consideration of "Java policy change" risk
Top 7 Torrent Sites Of All Time