Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Created August 28, 2024 22:50
Show Gist options
  • Save sandipchitale/24399a4ea5f450665a7a0ef34b2feeef to your computer and use it in GitHub Desktop.
Save sandipchitale/24399a4ea5f450665a7a0ef34b2feeef to your computer and use it in GitHub Desktop.
Uniform INteger Count between inclusive Range #facebook
static int getUniformIntegerCountInInterval(long A, long B) {
int count = 0;
int lenA = String.valueOf(A).length();
int lenB = String.valueOf(B).length();
for (int i = lenA; i <= lenB; i++) {
String spacesOfLengthI = String.format("%" + i + "s", " ");
for (int j = 1; j <= 9; j++) {
Long uniformNumber = Long.valueOf(spacesOfLengthI.replace(" ", String.valueOf(j)));
if (uniformNumber >= A && uniformNumber <= B) {
count++;
}
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment