701757. Numbers containing 1, 2 and 3

Easy

Numbers Containing 1, 2 and 3

Summary

The problem is to find all numbers from a given list that contain at least one digit 1, 2, and 3. This is an array manipulation problem that involves using hash tables or mathematical concepts to solve the issue.

Detailed Explanation

To approach this problem, we can use a simple algorithm. We iterate over each number in the input list and check if it contains the digits 1, 2, and 3. We can do this by converting each number into a string and checking if the strings '1', '2', and '3' are present.

Here's a step-by-step breakdown of the solution:

  1. Iterate over each number in the input list.
  2. Convert each number into a string to make it easier to manipulate.
  3. Check if the strings '1', '2', and '3' are present in the converted string.
  4. If all three strings are found, add the original number to our result set.

Time complexity for this solution is O(n*m) where n is the size of the input list and m is the maximum length of a number in the list. Space complexity is O(1) as we only need a constant amount of space to store the result.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Integer> findNumbersContaining123(int[] nums) {
        List<Integer> result = new ArrayList<>();
        
        for (int num : nums) {
            String str = Integer.toString(num);
            
            if (str.contains("1") && str.contains("2") && str.contains("3")) {
                result.add(num);
            }
        }
        
        return result;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.