Python 1317.py
class Solution:
def getNoZeroIntegers(self, n: int) -> List[int]:
# stupid brute force
a = int(n)
while True:
a -= 1
b = n - a
if "0" not in str(a) and "0" not in str(b):
return [ a, b ]
Difficulty
and has an acceptance rate of about 59% (300k+ submissions).
Typical patterns to solve this problem:
- Math
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a class Solution with a single method getNoZeroIntegers. The method receives an integer n and returns a two-element list [a, b] such that a + b = n and neither a nor b contains the digit 0 in its decimal representation. The algorithm proceeds by decrementing a candidate value a from n-1 downward until the zero-free condition is satisfied for both numbers.
Key Steps
The method begins by copying the input value into a mutable variable.
a = int(n)
A perpetual loop is then entered, guaranteeing that the search continues until a valid pair is found.
while True:
Inside the loop the candidate a is reduced by one on each iteration, and the complementary value b is computed as the difference between the original n and the current a.
a -= 1
b = n - a
The core validation checks whether the string form of each number contains the character '0'. If both checks succeed, the method returns the pair.
if "0" not in str(a) and "0" not in str(b):
return [a, b]
Because the loop is infinite only until the condition is met, the function exits as soon as the first suitable pair is discovered.
Edge Cases Handled
When n is the smallest possible value that admits a solution (for example n = 2), the algorithm still works correctly. The first decrement yields a = 1 and b = 1; both numbers lack a zero digit, so the pair [1, 1] is returned immediately.
If n itself contains a zero digit, the algorithm does not rely on the representation of n for validation; it only inspects a and b. Consequently, inputs such as n = 101 are processed without special treatment, and the loop will eventually locate a pair like [11, 90]-but since 90 contains a zero, the loop continues until a zero-free complement is found, for instance [11, 90] is rejected and the search proceeds to [12, 89], which satisfies the condition.
The method also gracefully handles large values of n. Even when n has many digits, the conversion to string and the '0' not in check are performed only on the two candidate numbers per iteration, keeping memory usage modest.
Performance Optimizations Observed
Although the algorithm follows a straightforward brute-force pattern, it incorporates a few implicit efficiencies. The decrement of a by a single unit ensures that each possible pair is examined in a deterministic order, avoiding redundant calculations. By computing b as n - a rather than recomputing both numbers from scratch, the code eliminates an extra subtraction operation that would be required if both candidates were generated independently.
The condition if "0" not in str(a) and "0" not in str(b): leverages Python’s fast substring search on the string representation, which is generally faster than manually iterating over each digit. This early-exit check prevents unnecessary work after the first valid pair is found, terminating the loop immediately without scanning the remaining search space.
Finally, the use of a single while True loop with a direct return statement eliminates the need for additional flag variables or post-loop checks, streamlining control flow and reducing overhead.
ELI5 - Simplified Explanation
Imagine holding a big number like ten in your hands, and you want to split it into two smaller pieces. The rule is that neither of the pieces can have the number zero hiding inside them. To do this, the computer starts by picking a small piece and checking it. If that piece has a zero, the computer says no and tries a different, slightly smaller piece instead. Then it looks at the other piece that is left over to make sure it does not have a zero either. The computer keeps picking different pairs of pieces, one by one, just like trying on different shoes until it finds a pair that fits perfectly without any sneaky zeros anywhere. Once it finds two pieces that follow the rule, it sets them aside to show you that the puzzle is finished.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||||
| www | AlgoMonster | C++ | Java | Python | TS | ||||||
| WalkCCC | C++ | Java | Python | ||||||||
| leetcode.ca | C++ | Java | Go | Python | Rust | TS | |||||
| Progiez | C++ | Java | Python | ||||||||
| github | BigEggStudy | C# | |||||||||
| black-shadows | C++ | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JS | ||||||||||
| keineahnung2345 | C++ | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||||
| WalkCCC | C++, Python, Java | ||||||||||
| leetcode.ca | C++, Go, Java, Python, Rust, TypeScript | ||||||||||
| Progiez | C++, Java, Python | ||||||||||
| BigEggStudy | C# | ||||||||||
| black-shadows | C++, C++ | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JavaScript | ||||||||||
| keineahnung2345 | C++ | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java |
Videos
Solved On The Same Day
- problem 1417: Reformat The String
- problem 1556: Thousand Separator
- problem 1790: Check if One String Swap Can Make Strings Equal
- problem 2259: Remove Digit From Number to Maximize Result
- problem 2582: Pass the Pillow
- problem 2605: Form Smallest Number From Two Digit Arrays
- problem 2729: Check if The Number is Fascinating
- problem 881: Boats to Save People
- problem 1503: Last Moment Before All Ants Fall Out of a Plank
- problem 1578: Minimum Time to Make Rope Colorful
- problem 1685: Sum of Absolute Differences in a Sorted Array
- problem 1701: Average Waiting Time
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality