---
title: "Beginner-Friendly Guide: LeetCode 1404 Binary String Reduction (C++, Python, JavaScript)"
url: "https://binary.ph/2026/02/26/beginner-friendly-guide-leetcode-1404-binary-string-reduction-c-python-javascript/"
description: "Beginner-friendly guide to LeetCode 1404 Binary String Reduction across C++, Python, and JavaScript. Learn intuition and practical, efficient steps."
author: "BinaryPH"
published: "2026-02-26T02:31:09+00:00"
modified: "2026-02-26T02:31:09+00:00"
tags: ["Main"]
---

# Beginner-Friendly Guide: LeetCode 1404 Binary String Reduction (C++, Python, JavaScript)

**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:
- 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.
