English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to write a permutation algorithm in ABAP language. For a given set of letters, I need to determine the unique permutations as follows:

Example set includes [A, B, C, D]

Unique permutaions include: A, B, C, D, AB, AC, AD, BC, BD, CD, ABC, ACD, BCD, ABCD.

Note: AB = BA.

2007-08-06 03:15:15 · 1 answers · asked by Dieselnc 1 in Computers & Internet Programming & Design

Thanks for the answer McFate! I'm not familiar w/Java so it’s somewhat difficult to follow your example. Can you explain how the solution would look if the results were in ascending order?

2007-08-08 16:51:37 · update #1

1 answers

Don't have ABAP handy, but here it is in Java. It's a trivial program and should be easy to translate as long as you can do 2^x (1<
===========================

public class MyClass {
public static void main(String[] args) {
String c = "ABCD";

int numChars = c.length();
int permutations = 1<
// For each permutation
for (int i=1; i
// Print it out from the binary number
for (int j=0; j int jthBit = 1 << j;
boolean jthBitIsSet = (i & jthBit) != 0;
if (jthBitIsSet) System.out.print( c.charAt(j));
}

System.out.println();
} } }

=================
Output:

A
B
AB
C
AC
BC
ABC
D
AD
BD
ABD
CD
ACD
BCD
ABCD

=================
How it works:

There are 2^n combinations of each character from the original string being either present or not-present in any solution, so one can represent each solution as a different binary number with one digit (1/0) corresponding to each character of the original string.

I use the loop iterator "i" to count up to 2^n (all binary numbers that can be represented in a number of digits equal to the number of characters in the original string). Each value of "i" represents one member of the permutations set.

The interior loop "j" is used to print each character corresponding to a 1-bit in "i", to print out the permutation that corresponds to the binary number in "i".

2007-08-06 03:35:48 · answer #1 · answered by McFate 7 · 2 0

fedest.com, questions and answers