// CodeEval String Permutations
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
class Main {
public static void main(String[] args) {
str_perm.main(args);
}
}
class str_perm {
public static void main(String[] args) {
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
if (LOCAL_TEST)
CodeEvalGetInput("e:\\zin.txt");
else
CodeEvalGetInput(args[0]);
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
Initialize();
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split(";");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Initialize() {
}
private static void Process(String[] lineArray) {
String s = lineArray[0];
List<String> ss = GetPermutation(s);
Collections.sort(ss);
System.out.print(ss.get(0));
for (int i = 1; i < ss.size(); i++) {
System.out.print("," + ss.get(i));
}
System.out.println();
}
public static List<String> GetPermutation(String s) {
List<String> result = new ArrayList<String>();
Permutation("", s, result);
return result;
}
private static void Permutation(String prefix, String s, List<String> result) {
int n = s.length();
if (n == 0)
result.add(prefix);
else {
for (int i = 0; i < n; i++)
Permutation(prefix + s.charAt(i),
s.substring(0, i) + s.substring(i + 1), result);
}
}
}
No comments:
Post a Comment