Sequential Digits in Python
1 min read

Sequential Digits in Python

Problem:

An integer has sequential digits if and only if each digit in the number is one more than the previous digit.

Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.

Example:

Input: low = 100, high = 300

Output: [123,234]

Constraints:

10 <= low <= high <= 10^9

Solution:

class Solution:
  def sequentialDigits(low, high):
  result = []
  start = int(str(low)[0])
  for val in range(1, len(str(low))):
    new_val = start%10 + 1
    start = start*10 + new_val
  if start > high:
    return result

  result.append(start)
  
  while result[-1] <= high:
    temp = str(result[-1])
    next_elem = int(temp[-1]) + 1
    
    if next_elem > 9:
      next_greater = 0
      for index in range(len(temp) + 1):
        next_greater = next_greater*10 + (index+1)
    else:
      next_greater = int(temp[1:]) * 10 + next_elem
    
    if next_greater <= high:
      result.append(next_greater)
    else:
      break
  
  final_result = []
  for val in result:
    if '0' not in str(val) and val >= low:
      final_result.append(val)
  return final_result


low = 100
high = 300
print(Solution.sequentialDigits(100, 300))