Solving Jumble:
# Find a common word that is a jumble of the letters RGYUEN
Solution:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String a = "RGYUEN";
StringBuilder b = new StringBuilder();
List<String> permutations = new ArrayList<String>();
boolean[] used = new boolean[a.length()];
permute(a, b, used, 0, a.length()-1, perumations);
for (int i = 0; i < permutations.size(); i++) {
if (isValid(permutations.get(i))) {
System.out.println(permutations.get(i));
}
}
}
public static void permute(String a, StringBuilder b, boolean[] used, int start, int end, List<String> permutations) {
if (b.length() == end - start + 1) {
permutations.add(b.toString());
return;
}
for (int i = start; i <= end; i++) {
if (used[i]) continue;
used[i] = true;
b.append(a.charAt(i));
permute(a, b, used, start, end, permutations);
b.deleteCharAt(b.length()-1);
used[i] = false;
}
}
public static boolean isValid(String s) {
if ((s.charAt(0) == 'G' || s.charAt(2) == 'Y' || s.charAt(5) == 'R') &&
(s.charAt(0) == 'G' || s.charAt(2) == 'Y' || s.charAt(5) == 'R') &&
(s.charAt(0) == 'G' || s.charAt(2) == 'Y' || s.charAt(5) == 'R'))
return true;
return false;
}
}
Answer: Gurney
No comments:
Post a Comment