Separate the Digits in an Array (LeetCode 2553): Solution and Explanation

Easy
Array
Simulation

You get an array of positive integers. Return one flat array holding every digit of every number, with the numbers kept in their original order and the digits of each number kept in the order they are written.

Intuition

The answer is just the numbers written next to each other and then read one digit at a time. If you print [13,25,83,77] as 13 25 83 77 and drop the spaces, you get 13258377, which is exactly the answer [1,3,2,5,8,3,7,7].

So the task is to cut each number into digits without changing their order. Converting the number to a string does that for free: the string "10921" already lists its digits from left to right, zeros included.

The arithmetic route with num % 10 also works, but it peels digits off from the right. You then have to reverse each small group before appending it, which is an easy step to forget.

Approach: String split per number

  1. Create an empty list answer.
  2. For each num in nums, from left to right, convert it to its decimal string.
  3. For each character of that string, append its integer value to answer.
  4. Return answer once every number has been processed.

Step-by-step example

nums = [13,25,83,77]
  1. Start with answer = [].
  2. num = 13 becomes "13", so append 1 and 3. answer = [1,3].
  3. num = 25 becomes "25", so append 2 and 5. answer = [1,3,2,5].
  4. num = 83 becomes "83", so append 8 and 3. answer = [1,3,2,5,8,3].
  5. num = 77 becomes "77", so append 7 and 7. The loop ends and the function returns [1,3,2,5,8,3,7,7].

Code: Python, Java, C++

1class Solution:
2    def separateDigits(self, nums: List[int]) -> List[int]:
3        answer = []
4        for num in nums:
5            digits = str(num)
6            for ch in digits:
7                answer.append(int(ch))
8        return answer
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n * d)

Each of the n numbers is converted once and each of its d digits (at most 6 here) is appended once, so the work is proportional to the total number of digits.

Space

O(n * d)

The output holds every digit. Apart from the output, only one short string per number is used.

Other approaches

Modulo and reverse (time O(n * d), space O(n * d))

Extract digits with num % 10 and num //= 10, collect them in a small buffer, then reverse the buffer before appending it. It avoids string conversion but needs the extra reverse step.

Join then split (time O(n * d), space O(n * d))

Join all numbers into one long string, then map every character to an integer. It is a one-liner in most languages and gives the same answer, at the cost of building one large temporary string.

Common mistakes

  • Appending digits from num % 10 without reversing them, which turns 13 into [3,1].
  • Dropping zeros inside a number such as 10921 or 100000. Every written digit belongs in the answer.
  • Appending the character itself instead of its integer value, which returns '1' or the character code 49 instead of 1.

Frequently asked questions

Is converting to a string allowed in an interview?

Usually yes for an Easy problem like this one. If the interviewer asks for pure arithmetic, use the modulo loop and reverse each number's digits before appending them.

Why do the digits of one number never mix with the next number's digits?

The loop finishes appending all digits of the current number before it moves on, so each number's digits form one contiguous block in the answer.

How long can the answer get?

Every value is at most 100000, which has 6 digits, so with 1000 numbers the answer has at most 6000 entries.