Big probem for me to change the code.

G

George

The below is my code to print permuation of 123:

public class per {

public static void main(String[] args) {

permutation("123", "");

}

private static void permutation(String p1, String p2) {
if (p1.length() == 0)
System.out.println(p2);
else {
for (int i = 0; i < p1.length(); i++)
permutation(p1.substring(0,i) + p1.substring(i+1), p2 + p1.charAt(i));
}
}

}

The results is:
123
132
213
231
312
321

What I want to do is that when (p1.substring(0,i) + p1.substring(i+1)) is
"2", do next line.
For example, that condition comes true when doing the line of "132", the
remaining process to produce "132" skipped and to do next line "213".

Any idea would be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top