Created
September 29, 2017 05:56
-
-
Save anil477/b05a6961365f79ab41e0e32bf0fe9783 to your computer and use it in GitHub Desktop.
ColourFull Number - https://www.interviewbit.com/problems/colorful-number/
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
public class Solution { | |
public int colorful(int a) { | |
String input = String.valueOf(a); | |
int N = input.length(); | |
HashSet<Long> productOf = new HashSet<>(); | |
List<String> results = new ArrayList<>(); | |
for (int m = 1; m <= N; m++) { | |
for (int i = 0; i + m <= N; i++) { | |
results.add(input.substring(i, i + m)); | |
} | |
} | |
for (String str : results) { | |
long product = 1; | |
for (int i = 0; i < str.length(); i++) { | |
product = product * Long.valueOf(str.substring(i, i + 1)); | |
} | |
if (productOf.contains(product)) | |
return 0; | |
productOf.add(product); | |
} | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment