Last active
August 26, 2023 17:39
-
-
Save thmain/ccd5a6addedeea0aebd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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