chetan
BAN USER- 0of 0 votes
AnswersIf given a number find a number if it is colorful. A number is said to be colorful if all its possible unique permutations multiplication result different.
- chetan in United States
Eg: if n = 1234 then permutations are (1,2),(1,3),(1,4), (2,3),(2,4),(3,4),(1,2,3), (1,2,4), (2,3,4). That's it, no other combination. Find the multiplication of digits in each combination and if any of them repeats then number is not colorful.| Report Duplicate | Flag | PURGE
Epic Systems Software Developer Algorithm
public class PermutationOfString {
public static void main(String[] args){
String s = "q1zRe";
Set<String> output = findPermutations2(s);
for(String w: output)
{
System.out.println(w);
}
}
public static Set<String> findPermutations(String s){
Set output = new HashSet();
if(s == null){
return null;
}
else if(s.length() == 0)
{
output.add("");
return output;
}
char ch = s.charAt(0);
String remString = s.substring(1);
Set<String> remainingSet = findPermutations(remString);
for(String newString : remainingSet)
{
for(int i = 0; i <= newString.length(); i++)
{
output.add(addCharAtPosition(newString, ch, i));
}
}
return output;
}
public static String addCharAtPosition(String s, char ch, int i){
return (s.substring(0, i) + ch + s.substring(i));
}
public static Set<String> findPermutations2(String s){
Set output = new HashSet();
HashMap<Integer, Character> indices = new HashMap<Integer, Character>();
StringBuilder indexString = new StringBuilder();
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z')
{
indices.put(i, s.charAt(i));
indexString.append(i);
}
}
int index1 = 0, index2 = 0;
char[] sArray = s.toCharArray();
Set<String> permutations = findPermutations(indexString.toString());
for(String perm : permutations){
sArray = s.toCharArray();
for(int i = 0; i<indexString.length(); i++)
{
index1 = Integer.parseInt(perm.charAt(i)+ "");
index2 = Integer.parseInt(indexString.charAt(i) + "");
sArray[index2] = indices.get(index1);
}
output.add(new String(sArray));
}
return output;
}
}
I am assuming it will be like convert:
from:
1
/\
2 3
/ \
4 5
/ \
6 7
/ \
8 9
To:
8
/\
6 9
/ \
4 7
/ \
2 5
/ \
1 3
Java code:
public static void ReverseLeftNode(Node node)
{
if(node.left == null)
{
head = node;
return;
}
Node temp = node.right;
ReverseLeftNode(node.left);
node.left.right = temp;
node.left.left = node;
}
note: some modification in one of the answers above.
- chetan February 28, 2015Prime factors will always be same. I am not sure I quite got your solution. Suppose I try for number 9234.
Prime factors are
3 * 3
2
3
2 * 2
But its still colorful number.
Possible combinations are {92, 93, 94, 23, 24, 34, 923, 924, 934, 234}
None of the combination pair having same product.
- chetan March 01, 2015