forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumbersAtNGivenDigitSet.java
35 lines (30 loc) · 963 Bytes
/
NumbersAtNGivenDigitSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Time complexity is O(nLen * dLen)
class NumbersAtNGivenDigitSet {
public int atMostNGivenDigitSet(String[] digits, int n) {
String nStr = n +"";
int nLen = nStr.length();
int noOfDigits = digits.length;
int total = 0;
// X, XX, XXX ...
for(int i=1;i<nLen;i++){
total += Math.pow(noOfDigits, i);
}
for(int i=0;i< nLen;i++){
boolean hasSameNo = false;
for(String digit : digits) {
if(digit.charAt(0) < nStr.charAt(i)){
total += Math.pow(noOfDigits, nLen - i -1);
} else if(digit.charAt(0) == nStr.charAt(i)){
hasSameNo = true;
if(i == nLen -1){
total++; // ["3", "4"] 4
}
}
}
if(!hasSameNo){
return total;
}
}
return total;
}
}