Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Stepping Numbers Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Stepping Numbers

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given two numbers begin and end, find all the stepping numbers in the range [begin, end]. A positive integer is called a stepping number if the adjacent digits have a difference of 1. All single-digit numbers are stepping numbers. 123, 121, 654, 5 are all stepping numbers. 124, 122, 46 are not stepping numbers.

Naive Approach

We can iterate over all the numbers in the range [begin, end] and check if each number is a “Stepping Number” or not.

Analysis

  • Time Complexity: O((begin - end + 1) * log (numbers in the range))
  • Space Complexity: O(1)

Implementation

C++
bool isStepping(int n){
	string digits = to_string(n);
	int len = digits.size();
	for(int i = 1; i < len; i++) {
		if(abs(digits[i] - digits[i - 1]) != 1) {
			return false;
		}
	}
	return true;
}
vector<int> getSteppingNumbers(int begin, int end) {
	vector<int> answer;
	for(int i = begin; i <= end; i++) {
		if(isStepping(i)) {
			answer.push_back(i);
		}
	}
	return answer;
}
Java
class Solution {
	boolean isStepping(int n) {
		String digits = Integer.toString(n);
		int len = digits.length();
		for(int i = 1; i < len; i++) {
			if(Math.abs(digits.charAt(i) - digits.charAt(i - 1)) != 1) {
				return false;
			}
		}
		return true;
	}
	List<Integer> getSteppingNumbers(int begin, int end) {
		List<Integer> answer = new ArrayList<>();
		for(int i = begin; i <= end; i++) {
			if(isStepping(i)) {
				answer.add(i);
			}
		}
		return answer;
	}
}

Optimal Approach:

We will try to model this problem into a graph traversal problem. Consider that the starting node is 0, and from it, we try to move to all possible reachable nodes in our given range. 

  • From 0, we can move to 1, 2, 3, 4,…, 9
  • From 1, we can move to 12, 10
  • From 2, we can move to 23, 21
  • And so on….

In this manner we can traverse on the reachable nodes and store those which are in our range. As soon as we go outside our range, we break the iteration.

Analysis:

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Implementation:

C++
vector<int> getSteppingNumbers(int begin, int end) {
	int initialValue[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	vector<int> answer;
	queue<int> q;
	for(int i = 1; i <= 9; i++) {
		q.push(i);
	}
	if(begin == 0) {
		answer.push_back(begin);
	}
	while(!q.empty()) {
		int node = q.front();
		q.pop();
		if(node >= begin && node <= end) {
			answer.push_back(node);
		}
		int child, lastDigit = node % 10;
		node *= 10;
		if(lastDigit) {
			child = node + initialValue[lastDigit - 1];
			if(child <= end) {
				q.push(child);
			}
		}
		if(lastDigit != 9) {
			child = node + initialValue[lastDigit + 1];
			if(child <= end) {
				q.push(child);
			}
		}
	}
	return answer;
}

 Java

class Solution {
	List<Integer> getSteppingNumbers(int begin, int end) {
    	int initialValue[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		List<Integer> answer = new ArrayList<>();
		Queue<Integer> q = new LinkedList<>();
		for(int i = 1; i <= 9; i++) {
			q.add(i);
		}
		if(begin == 0) {
			answer.add(begin);
		}
		while(!q.isEmpty()) {
			int node = q.poll();
			if(node >= begin && node <= end) {
				answer.add(node);
			}
			int child, lastDigit = node % 10;
			node *= 10;
			if(lastDigit != 0) {
				child = node + initialValue[lastDigit - 1];
				if(child <= end) {
					q.add(child);
				}
			}
			if(lastDigit != 9) {
				child = node + initialValue[lastDigit + 1];
				if(child <= end) {
					q.add(child);
				}
			}
		}
		return answer;
	}
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Valid Path
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet