|
| 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] |
0 commit comments