Ir al contenido principal

Permutaciones con String en Java

A continuación una clasesilla que implemente, para realizar la permutación de String en Java.

La idea es la siguiente, si usted le pasa la palabra "cat", la clase hace cat.length! permutaciones con las diferentes combinaciones para esta cadena, es decir:

[act, atc, cat, cta, tac, tca]

A continuación el código (perdón por los comentarios en ingles):




/**
* Useful class to figure out the permutation of the strings.
* @author jsanca
*
*/
public class StringPermutations {

/**
* Use this method to figure out the permutation of the String.
* @param string
* @return collection with the permutation.
*/
public static Collection doPermutations(String string) {

Collection permutationsCollection = new TreeSet();

// verify the trival case, the permutation of 1 is 1, anything else, will use the permutation.
permutationsCollection.addAll((string.length() == 1) ? Arrays.asList(string)
: permutations(string, string.length()));

return permutationsCollection;
} // permutations.

/// apply the shiftleft over the string
private static String shiftleft(String s) {

return new StringBuilder(s.substring(1)).append(s.substring(0, 1))
.toString();
} // shiftleft.

private static Collection permutations(String string, int depth) {

Collection permutations = new ArrayList();

if (depth >= 1) {

// do the permutations of the current string.
permutations.addAll(permutations(string));
// do the permutations of the next string.
permutations.addAll(permutations(shiftleft(string), --depth));
}

return permutations;
} // permutation.

private static Collection permutations(String string) {

Collection permutations = new HashSet();
String subSet = null;

// stop condition, this is the trivial case for the items.
if (string.length() == 2) {

// basic permutation.
permutations(permutations, string.substring(0, 1), string
.substring(1));
} else {

// do the permutations merging the index with the rest of the subset.
for (int i = 0; i < string.length(); ++i) {

subSet = buildString(string, i);
// combinate the index with the permutation of the rest of the subset.
permutations(permutations, string.subSequence(i, i + 1),
permutations(subSet, subSet.length()));
}
}

return permutations;
} // permutations.

// create a string except the "i" index.
private static String buildString(String string, int i) {

String buildString = null;

if (i == 0) {

buildString = string.substring(1);
} else {

if (i == string.length() - 1) {

buildString = string.substring(0, string.length() - 1);
} else {

buildString = new StringBuilder(string.substring(0, i)).append(
string.substring(i + 1)).toString();
}
}

return buildString;
} // buildString.

// BASIC CASE OF PERMUTATION.
private static void permutations(Collection permutations,
CharSequence permutation1, CharSequence permutation2) {

permutations.add(new StringBuilder(permutation1).append(permutation2)
.toString());
permutations.add(new StringBuilder(permutation2).append(permutation1)
.toString());
} // permutations.

// SCALAR + SET PERMUTATION.
private static void permutations(Collection permutations,
CharSequence aPermutation, Collection permutation) {

CharSequence otherPermutation = null;

for (Iterator iterator = permutation.iterator(); iterator
.hasNext();) {

otherPermutation = iterator.next();
permutations.add(new StringBuilder(aPermutation).append(
otherPermutation).toString());
}
} // permutations.


} // E:O:F:Permutations.


/**
* Util class for Math operations.
* @author jsanca
*
*/
public class MathUtil {

/**
* Calculate the factorial of the number, if it is negative, it will figure out like C++ uint.
* @param number Some number
* @return if the number is 0 will returns 1, in other case will returns the factorial:
* n! = 1 * 2 * 3 * 4 * ... * (n-1) * n
*/
public static double factorial(double number) {

return (0 >= number) ? 1 :
number * factorial(number - 1);
}


}


Comentarios

Carlos P. dijo…
Maldito!
Acá va en Perl uno que hice.
..Espero que esta vara no salga muy despichado.

open(FILE_OUTPUT, ">@ARGV[0]"); #in case the output is
$| = 1;

&permutate("abcd", 1);

#GLOBALS
#stores the entry length, so that it can be utilized after the recursive call
$entryDepth = undef;
#string vector of size n (n = string length) used to keep the record of the last permutations
@permStateVector = ();

#calls permutateRec with a fixed deph and initializes the permutation state vector
#@toFile is a boolean parameter that if true permits writing to a file
sub permutate{
my $string = shift;
my $toFile = shift;
my $depth = length($string);
@permStateVector = &fillPermStateVector(0, $string, @permStateVector);
&permutateRec($string, $depth, $toFile);
}

#gets all the permutations in a string and prints them out.
#Printing to a file optional
sub permutateRec{
my $string = shift;
my $depth = shift;
my $toFile = shift;

for (my $i = 1; $i <= $depth; $i++){
$entryDepth = $depth if ($entryDepth == undef);

if ($depth == 1){
my $entry = &permutateEntry(length($string) - $entryDepth, @permStateVector);
print $entry."\n";
print FILE_OUTPUT $entry."\n" if ($toFile);

$entryDepth = undef;
}
&permutateRec($string, $depth - 1, $toFile);
}
}

#modifies the permutation state vector by changing an entry in a specific position
# with a new permutation. After the single entry is modified, the rest of the entries
# in the vector are filled with the new entry (starting from that specific position)
sub permutateEntry{
my $pos = shift;
@permStateVector = @_;
my $entry = @permStateVector[$pos];

$entry = substr($entry, 0, $pos).
&shiftLeftCharsStr(substr($entry, $pos), 1);

@permStateVector = &fillPermStateVector($pos, $entry, @permStateVector);

return $entry;
}

#shift-lefts a string a specific number of characters
sub shiftLeftCharsStr{
my $string = shift;
my $charsNumber = shift;
my $newString = substr($string, $charsNumber).
substr($string, 0, $charsNumber);
return $newString;
}

#fills the permutation state vector with the same string starting from a specific position
sub fillPermStateVector{
my $startingPos = shift;
my $string = shift;
my @permStateVector = @_;
for (my $i = $startingPos; $i < length($string); $i++){
@permStateVector[$i] = $string;
}
return @permStateVector;
}
Carlos P. dijo…
que madre. Al final se perdieron todos los espacios :S

Entradas más populares de este blog

Impensando acerca de las referencias en Java

Fue hace ya algún tiempo que pase un rato discutiendo con algunos compañeros acerca de si existe o no el paso por referencia; el discurso fue mucho hacia que en Java el comportamiento, en el supuestamente pasamos por referencia un objeto y por valor los objetos primitivos creo mucha polémica. Para ubicarnos en contexto veamos el siguiente ejemplo. public static void main(String[] args) { int value = 10; changeValue(value); System.out.println("value = " + value); User user = new User(); Name name = new Name(); user.setName(name); name.setName("jsanca"); name.setLastName("XXX"); user.setPassword("123queso"); System.out.println("user: " + user.getName().getName() + ", " + user.getName().getLastName() + ", " + user.getPassword()); changeValue1(user); System.out.println("user: " + user.getName().getName() + ", " + user.getName().getLastName() + ", " + user.ge...

Analizador de expresiones algebraicas recursivo decendente

Como les mencione en un post previo, estoy leyendo el libro el arte de programar en Java, el primer ejercicio consiste en un analizador de expresiones algebraicas recursivo descendente, el mismo consiste en la posibilidad de tomar una cadena que contenga una expresión matemática, la misma puede contener valores en punto flotante, sumar, restar, dividir, multiplicar, sacar exponente (potencia), uso de paréntesis para priorizar una operación, etc. A continuación clase a clase, con una pequeña explicación Lo primero que definiremos es una suite de excepciones para reportar errores, no tiene mucha ciencia, hay una para la division entre cero, cuando no existe una expresión valida, error de sintaxis o cuando los paréntesis no se encuentran balanceados, veamos package cap2; /** * Exception para reportar que hay al intentar dividir entre cero * * User: jsanca * Date: 4/16/13 * Time: 1:30 AM * @author jsanca */ public class DividedByZeroException extends RuntimeException { ...

Links acerca de usabilidad

Bueno esta haciendo un research acerca de usabilidad y decidi compartir algunos de los links mas interesantes: Este esta muy cool y dice por que son buenos, gmail #1: http://www.1stwebdesigner.com/design/well-designed-usable-sites/ Los mejores menus: http://www.kronikmedia.co.uk/blog/website-navigation-menu-design/3580/ Otro top ten: http://www.topsite.com/best/usability los CMS con mas usabilidad http://net.tutsplus.com/articles/web-roundups/top-10-most-usable-content-management-systems/ Las grandes companias que incorporan usabilidad en sus sistemas: http://www.siteiq.net/7806/the-2013-usability-top-10-ibm-leads-sap-soars-and-apple-screws-up-the-rankings-2 + Algo interesante: top ten de sitios de Universidades http://blog.thebrickfactory.com/2010/03/top-11-best-designed-university-websites/ Y estos son 10 videitos acerca de usabilidad: http://www.usefulusability.com/10-must-see-usability-videos/ Enjoy!