Skip to content

Commit ad863ad

Browse files
authored
Create count-numbers-with-unique-digits-ii.py
1 parent f66d74d commit ad863ad

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(blogb)
2+
# Space: O(1)
3+
4+
# hash table
5+
class Solution(object):
6+
def numberCount(self, a, b):
7+
"""
8+
:type a: int
9+
:type b: int
10+
:rtype: int
11+
"""
12+
def check(x):
13+
lookup = 0
14+
while x:
15+
if lookup&(1<<(x%10)):
16+
return False
17+
lookup |= (1<<(x%10))
18+
x //= 10
19+
return True
20+
21+
return sum(check(x) for x in xrange(a, b+1))
22+
23+
24+
# Time: O(blogb)
25+
# Space: O(logb)
26+
# hash table
27+
class Solution2(object):
28+
def numberCount(self, a, b):
29+
"""
30+
:type a: int
31+
:type b: int
32+
:rtype: int
33+
"""
34+
return sum(len(set(str(x))) == len(str(x)) for x in xrange(a, b+1))

0 commit comments

Comments
 (0)