A permutation is a way of arranging the objects in such a way so that no 2 different arrangements should have the same placement.
package org.wesome.dsalgo;
public class Permutation {
static int permutationCount = 0;
public static int permutation(String chosen, String remaining) {
int remainingLength = remaining.length();
if (remainingLength == 0) {
System.out.println("permutation is = " + chosen);
permutationCount++;
} else {
for (int i = 0; i < remainingLength; i++) {
permutation(chosen + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1, remainingLength));
}
}
return permutationCount;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class PermutationTest {
@Test
void permutationTest1() {
Permutation.permutationCount = 0;
String string = "wesome.org";
Assertions.assertEquals(3628800, Permutation.permutation("", string));
}
@Test
void permutationTest2() {
Permutation.permutationCount = 0;
String string = "1";
Assertions.assertEquals(1, Permutation.permutation("", string));
}
@Test
void permutationTest3() {
Permutation.permutationCount = 0;
String string = "123";
Assertions.assertEquals(6, Permutation.permutation("", string));
}
}
plugins {
id 'java'
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}