Skip to content

Commit c369b42

Browse files
committed
solve Sequential Digits
1 parent 5b14f61 commit c369b42

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

problems/sequential_digits.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
# Straight Forward Iterative.
6+
#
7+
# Time: O(1)
8+
# Space: O(1)
9+
#
10+
# Examples:
11+
# low = 100, high = 300
12+
# [123, 234]
13+
# low = 1000, high = 13000
14+
# [1234, 2345, 3456, 4567, 5678, 6789, 12345]
15+
# 10 <= low <= high <= 10^9
16+
def sequentialDigits(self, low: int, high: int) -> List[int]:
17+
def get_num_digits(num: int):
18+
# O(1) time, O(1) space.
19+
num_digits = 0
20+
while num:
21+
num //= 10
22+
num_digits += 1
23+
return num_digits
24+
25+
low_num_digits = get_num_digits(num=low)
26+
high_num_digits = get_num_digits(num=high)
27+
28+
def get_sequential_numbers(num_digits: int):
29+
# O(1) time, O(1) space
30+
# 1 <= num_digits <= 9
31+
for digit in range(1, 10 - num_digits + 1):
32+
num = 0
33+
for _ in range(num_digits):
34+
num = num * 10 + digit
35+
digit += 1
36+
yield num
37+
38+
# O(1) time, O(1) additional space
39+
ans = []
40+
for num_digits in range(low_num_digits, high_num_digits + 1):
41+
for num in get_sequential_numbers(num_digits=num_digits):
42+
if low <= num <= high:
43+
ans.append(num)
44+
45+
return ans
46+
47+
48+
class SolutionTwo:
49+
# Sliding Window.
50+
#
51+
# All sequential numbers are substrings of the string "123456789".
52+
# We slide over the string, with window length = number of digits, which itself
53+
# iterates from number of digits in low, and the number of digits in high.
54+
#
55+
# Time: O(1)
56+
# Space: O(1)
57+
def sequentialDigits(self, low: int, high: int):
58+
sample = "123456789"
59+
ans = []
60+
for length in range(len(str(low)), len(str(high)) + 1):
61+
for start in range(10 - length):
62+
num = int(sample[start : start + length])
63+
if low <= num <= high:
64+
ans.append(num)
65+
return ans
66+
67+
68+
class SolutionThree:
69+
# Pre-computation.
70+
# Time: O(1)
71+
# Space: O(1)
72+
def sequentialDigits(self, low: int, high: int):
73+
sequential_numbers = [
74+
12,
75+
23,
76+
34,
77+
45,
78+
56,
79+
67,
80+
78,
81+
89,
82+
123,
83+
234,
84+
345,
85+
456,
86+
567,
87+
678,
88+
789,
89+
1234,
90+
2345,
91+
3456,
92+
4567,
93+
5678,
94+
6789,
95+
12345,
96+
23456,
97+
34567,
98+
45678,
99+
56789,
100+
123456,
101+
234567,
102+
345678,
103+
456789,
104+
1234567,
105+
2345678,
106+
3456789,
107+
12345678,
108+
23456789,
109+
123456789,
110+
]
111+
112+
return [num for num in sequential_numbers if low <= num <= high]

tests/test_sequential_digits.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import unittest
2+
3+
from sequential_digits import Solution, SolutionTwo, SolutionThree
4+
5+
6+
class TestSequentialDigits(unittest.TestCase):
7+
def test_example_1(self):
8+
assert Solution().sequentialDigits(low=100, high=300) == [123, 234]
9+
assert SolutionTwo().sequentialDigits(low=100, high=300) == [123, 234]
10+
assert SolutionThree().sequentialDigits(low=100, high=300) == [123, 234]
11+
12+
def test_example_2(self):
13+
assert Solution().sequentialDigits(low=1000, high=13000) == [
14+
1234,
15+
2345,
16+
3456,
17+
4567,
18+
5678,
19+
6789,
20+
12345,
21+
]
22+
assert SolutionTwo().sequentialDigits(low=1000, high=13000) == [
23+
1234,
24+
2345,
25+
3456,
26+
4567,
27+
5678,
28+
6789,
29+
12345,
30+
]
31+
assert SolutionThree().sequentialDigits(low=1000, high=13000) == [
32+
1234,
33+
2345,
34+
3456,
35+
4567,
36+
5678,
37+
6789,
38+
12345,
39+
]

0 commit comments

Comments
 (0)