Summary: This guide explains how to solve LeetCode problem 1404, where you must count the number of steps to reduce a binary string to 1 using only two operations: divide by 2 when the number is even and add 1 when the number is odd. The solution avoids converting the full binary string to a numeric type and instead simulates operations on the string efficiently.
Problem Summary
- Input: A string s representing a positive integer in binary format.
- Goal: Return the total number of steps to reduce the number to 1.
- Operations: If even, divide by 2. If odd, add 1.
Intuition and Key Observations
- Divide by 2 in binary is equivalent to removing the trailing ‘0’. That is one step when the last digit is ‘0’.
- Adding 1 to an odd binary number (ending in ‘1’) causes a carry. That carry can ripple left through consecutive ‘1’ bits, turning them into ‘0’ until a ‘0’ is found or a new leading ‘1’ is created.
- Carry trick: Instead of performing repeated string modifications, track a single carry flag. Walk the string from right to left, excluding the most significant bit, and compute steps by combining the current bit value and the carry.
Algorithm Outline
- Initialize steps to 0 and carry to 0.
- Iterate from the last character to the second character (index length-1 down to 1).
- For each bit:
- If the bit is ‘0’ and there is no carry, it takes 1 step to divide by 2.
- If the bit is ‘0’ and carry is 1, the bit effectively becomes ‘1’, so it will take 2 steps: add 1 then divide.
- If the bit is ‘1’ and carry is 0, adding 1 will create a carry and takes 2 steps total (add then divide), and set carry to 1.
- If the bit is ‘1’ and carry is 1, then ‘1’+carry becomes ‘0’ with carry still 1, so this state costs 1 step (divide) and carry remains 1.
- After the loop, if carry is 1, add a final step for the leftmost carry resolution.
Walkthrough Example
Example: s = “1101”
- Start from the rightmost bit and use the carry method.
- Processing yields a total of 6 steps. The operations sequence is: add, divide, add, divide, divide, divide leading to 6 steps.
- This matches step by step simulation but uses O(n) time and O(1) extra space.
Compact Implementations
C++ implementation:
int numSteps(string s) { int n = s.size(), steps = 0, carry = 0; for (int i = n – 1; i >= 1; –i) { if (s[i] == ‘0’) steps += (carry == 0) ? 1 : 2; else { steps += (carry == 0) ? 2 : 1; carry = 1; } } return steps + carry; }
Python implementation:
def numSteps(s):
steps = 0
carry = 0
for ch in reversed(s[:-1]):
if ch == ‘0’:
steps += 1 if carry == 0 else 2
else:
steps += 2 if carry == 0 else 1
carry = 1
return steps + carry
JavaScript implementation:
function numSteps(s) {
let steps = 0, carry = 0;
for (let i = s.length – 1; i >= 1; i–) {
if (s[i] === ‘0’) steps += carry === 0 ? 1 : 2;
else { steps += carry === 0 ? 2 : 1; carry = 1; }
}
return steps + carry;
}
Time and Space Complexity
- Time complexity: O(n), where n is the length of the binary string. Each bit is processed once.
- Space complexity: O(1) additional space. Only a few integers for counters and the carry are used.
Tips and Common Pitfalls
- Do not convert the full binary string to an integer type for large inputs, as it may overflow typical numeric types.
- Remember to exclude the most significant bit from the main loop and handle any leftover carry after the loop.
- Test edge cases: s = “1” should return 0, s = “10” should return 1, and strings with long runs of trailing ones will exercise carries.
Conclusion: Tracking a carry and processing the binary string from right to left yields a simple, efficient solution to LeetCode 1404. The approach is language agnostic and easy to implement in C++, Python, or JavaScript while remaining optimal in time and space.

Leave a Reply