Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

May 27, 2009

program to find permutations

import java.util.ArrayList;
import java.util.List;

public class Permutation {

public static void permuteString(List permutations,
String beginningString, String endingString) {

String output = beginningString + endingString;
if (endingString.length() <= 1) {
permutations.add(output);
} else {
for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i)
+ endingString.substring(i + 1);

permuteString(permutations, beginningString
+ endingString.charAt(i), newString);

} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
}

public static void main(String[] args) {
List permutations = new ArrayList();
permuteString(permutations, "", "123456789");
for (String string : permutations) {
// System.out.println(output);
int first = Integer.parseInt(string.substring(0, 4));
int second = Integer.parseInt(string.substring(4, 5));
int third = Integer.parseInt(string.substring(5, 9));
if ((first * second) == third) {
System.out.println(string + "-->true");
}
}
}
}

1 comment:

Anonymous said...

Really.