Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/java/org/fundacionjala/coding/abel/MiddlePermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.fundacionjala.coding.abel;

import java.util.Arrays;

/**
* You are given a string s. Every letter in s appears once.
* Consider all strings formed by rearranging the letters in s. After ordering these strings in dictionary order,
* return the middle term. (If the sequence has a even length n, define its middle term to be the (n/2)th term.)
* Example
* For s = "abc", the result should be "bac".
* The permutations in order are:
* "abc", "acb", "bac", "bca", "cab", "cba"
* So, The middle term is "bac".
* Input/Output
* [input] string s
* unique letters (2 < length <= 26)
* [output] a string
* middle permutation.
*/
public final class MiddlePermutation {

/**
* Private Constructor.
*/
private MiddlePermutation() {

}

/**
* Returns the Permutation located at mid position of all permutations available in the string.
*
* @param string String.
* @return Permutation located at mid position.
*/
public static String findMidPerm(String string) {
int length = string.length();
char[] currentCharArray = string.toCharArray();
Arrays.sort(currentCharArray);
char[] resultCharArray = new char[currentCharArray.length];
int half = length / 2;
int last = length - 1;
for (int i = last, j = 0; j < half - 1; i--, j++) {
resultCharArray[i] = currentCharArray[j];
}
if (length % 2 != 0) {
resultCharArray[0] = currentCharArray[half];
resultCharArray[1] = currentCharArray[half - 1];
for (int i = last, j = 2; i > half; i--, j++) {
resultCharArray[j] = currentCharArray[i];
}
} else {
resultCharArray[0] = currentCharArray[half - 1];
resultCharArray[half] = currentCharArray[half];
for (int i = last, j = 1; i > half; i--, j++) {
resultCharArray[j] = currentCharArray[i];
}
}
StringBuilder sb = new StringBuilder();
for (char c : resultCharArray) {
sb.append(c);
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.fundacionjala.coding.abel;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* This class test Middle Permutation Class.
*/
public class MiddlePermutationTest {

/**
* Test for Private Constructors.
* @throws NoSuchMethodException Thrown when a particular method cannot be found.
* @throws IllegalAccessException Thrown when an application tries to reflectively create an instance.
* @throws InvocationTargetException thrown by an invoked method or constructor.
* @throws InstantiationException Thrown when an application tries to create an instance of a class
* using the {@code newInstance} method in class.
*/
@Test
public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
Constructor<MiddlePermutation> constructor = MiddlePermutation.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}

/**
* Provided and new tests.
*/
@Test
public void basicTests() {
assertEquals("bac", MiddlePermutation.findMidPerm("abc"));
assertEquals("bdca", MiddlePermutation.findMidPerm("abcd"));
assertEquals("cbeda", MiddlePermutation.findMidPerm("abcde"));
assertEquals("cfedba", MiddlePermutation.findMidPerm("abcdef"));
assertEquals("dcgfeba", MiddlePermutation.findMidPerm("abcdefg"));
assertEquals("dhgfecba", MiddlePermutation.findMidPerm("abcdefgh"));
assertEquals("edihgfcba", MiddlePermutation.findMidPerm("abcdefghi"));
assertEquals("ejihgfdcba", MiddlePermutation.findMidPerm("abcdefghij"));
assertEquals("fekjihgdcba", MiddlePermutation.findMidPerm("abcdefghijk"));

}
}