Epic Systems Interview Question
Developer Program EngineersCountry: United States
public static void main(String[] args) {
Set<String> stringSet = new HashSet<String>();
getPermutationOfString("tree".toCharArray(), 0, stringSet);
printSets(stringSet);
}
public static void getPermutationOfString(char[] a, int i, Set<String> stringSet) {
if (i == a.length) {
stringSet.add(new String(a));
} else if (i < a.length) {
for (int j = i; j < a.length; j++) {
swap(a, i, j);
getPermutationOfString(a, i + 1, stringSet);
swap(a, i, j);
}
}
}
public static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void printSets(Set<String> strs) {
for (String string : strs) {
System.out.println(string);
}
}
//Note : Here Repetations allowed
// Means when same char comes again then same word would be repeated.
package recursion;
public class Permutation {
public static void main(String[] args) {
String str = "tree";
//permu(str, str.length(), "");
combi(str, str.length(), "");
}
private static void permu(String str, int len, String out) {
if(out.length() == len){
System.out.println(out);
return;
}
for(int i = 0;i < str.length(); i++){
permu(str, len, out+str.charAt(i));
}
}
private static void combi(String str, int len, String out) {
if(out.length() == len){
System.out.println(out);
return;
}
for(int i = 0;i < str.length(); i++){
combi((str.substring(0,i)+""+str.substring(i+1)), len, out+str.charAt(i));
}
}
}
/*Answers
tree
tree
tere
teer
tere
teer
rtee
rtee
rete
reet
rete
reet
etre
eter
erte
eret
eetr
eert
etre
eter
erte
eret
eetr
eert*/
import java.util.LinkedList;
public class hello{
public static void main(String args[]){
String str = "tree";
LinkedList<String> permutation = new LinkedList<>();
LinkedList<String> combinator = new LinkedList<>();
int i=0;
while(i<str.length()){
char s = str.charAt(i);
permutation.add(s+"");
i++;
}
for(int j=0;j<permutation.size();j++){
String sr = permutation.get(j);
permutation.remove(j);
combinator.addAll(permutation);
combinator.add(j,sr);
permutation.add(j, sr);
}
System.out.print(combinator.toString());
}
}
import java.util.LinkedList;
public class hello{
public static void main(String args[]){
String str = "tree";
LinkedList<String> permutation = new LinkedList<>();
LinkedList<String> combinator = new LinkedList<>();
int i=0;
while(i<str.length()){
char s = str.charAt(i);
permutation.add(s+"");
i++;
}
for(int j=0;j<permutation.size();j++){
String sr = permutation.get(j);
permutation.remove(j);
combinator.addAll(permutation);
combinator.add(j,sr);
permutation.add(j, sr);
}
System.out.print(combinator.toString());
}
}
Generates only :[t, r, e, e, r, e, e, t, e, e, t, r, e, t, r, e]
static HashSet<String> result = new HashSet<String>();
public static void wordPerm(String prefix, String input){
int len = input.length();
if (len == 0){
result.add(prefix);
}
else {
for (int i=0; i<len; i++){
wordPerm(prefix+input.charAt(i), input.substring(0,i)+input.substring(i+1));
}
}
}
static HashSet<String> result = new HashSet<String>();
public static void wordPerm(String prefix, String input){
int len = input.length();
if (len == 0){
result.add(prefix);
}
else {
for (int i=0; i<len; i++){
wordPerm(prefix+input.charAt(i), input.substring(0,i)+input.substring(i+1));
}
}
}
- kyduke April 16, 2015