d41515
BAN USER
Comments (5)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
This does not work. issue is that if the char is 8 or 9, then you need to add 1 to the higher char.
Here is my solution:
public class Scratch {
public static int luckNumber(int number){
return lucknum(number, 0, 0);
}
private static int lucknum(int num, int total, int index){
int remainder = num % 10;
int leftover = num / 10;
if(remainder <= 4){
remainder = 4;
}else if(remainder <=7){
remainder = 7;
}else if(remainder >7){
remainder = 4;
leftover +=1; // this is the critical, carry 1 to higher digit.
}
total += remainder * (int)Math.pow(10, index);
if(leftover > 0){
total = lucknum(leftover, total, ++index);
}
return total;
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Using Aho-Corasick trie would be the best approach. Build trie using all directionally. Input each words and return matched. For example, input wheelchair will return wheel, chair, wheelchair, heel, hair, eel, air, he etc. You know what to do next.
- d41515 November 07, 2014