By Vivek - April 19, 2020
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.
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)) Related Posts
- K-Concatenation Maximum Sum in Python
- Maximum Subarray Sum with One Deletion in Python
- Reverse Parentheses Substrings in Python
- Count Servers that Communicate in Python
- Count Square Submatrices with All Ones in Python
- Find Winner on a Tic Tac Toe Game in Python
- Minimum Absolute Difference in Python
- Minimum Time Visiting All Points in Python
- Number of Burgers with No Waste of Ingredients in Python
- Search Suggestions System in Python