Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active August 26, 2023 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/ccd5a6addedeea0aebd9 to your computer and use it in GitHub Desktop.
Save thmain/ccd5a6addedeea0aebd9 to your computer and use it in GitHub Desktop.
import java.util.HashSet;
public class ColorfulNumbers {
public boolean isColorful(int number){
HashSet<Integer> set = new HashSet<>();
String colorString = String.valueOf(number);
int length = colorString.length();
for(int i = 1; i < length; i++){
for (int j = 0; j+i <= length; j++){
String sub = colorString.substring(j, j+i);
int product = getProduct(Integer.parseInt(sub));
if(set.contains(product))
return false;
else{
set.add(product);
}
}
}
return true;
}
private static int getProduct(int digits) {
if(digits < 10) return digits;
int num = digits;
int product = 1;
while(num > 0){
product = product * (num % 10);
num = num / 10;
}
return product;
}
public static void main(String[] args) {
ColorfulNumbers c = new ColorfulNumbers();
System.out.println(" 326 Colorful- " + c.isColorful(326));
System.out.println(" 32458 Colorful- " + c.isColorful(3245));
System.out.println(" 32458 Colorful- " + c.isColorful(32458));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment