Home/Learn/String Parsing & Integer Conversion

Pattern Guide

String Parsing & Integer Conversion

"atoi, add binary/strings, multiply strings, number to/from base."

String parsing problems convert between string and numeric representations. Key patterns: atoi (handle leading whitespace, sign, overflow, non-digit stop), add binary/strings (digit-by-digit from end with carry), multiply strings (grade-school multiplication), number base conversion. These require careful handling of edge cases: empty string, overflow, leading zeros, negative numbers.

11 min readdp problems →

Problems you can solve with this pattern

4 problems · click any to start solving

All dp
1String to Integer (atoi)MediumSolve
2Add BinaryEasySolve
3Add StringsEasySolve
4Integer to English WordsHardSolve
atoi and add binary templates
// String to integer (atoi)
function myAtoi(s) {
    let i = 0, sign = 1, result = 0;
    const MAX = 2147483647, MIN = -2147483648;
    // Skip whitespace
    while (i < s.length && s[i] === ' ') i++;
    // Sign
    if (i < s.length && (s[i] === '+' || s[i] === '-'))
        sign = s[i++] === '-' ? -1 : 1;
    // Digits
    while (i < s.length && s[i] >= '0' && s[i] <= '9') {
        const digit = s[i++] - '0';
        if (result > Math.floor((MAX - digit) / 10)) // overflow check
            return sign === 1 ? MAX : MIN;
        result = result * 10 + digit;
    }
    return sign * result;
}

// Add two binary strings
function addBinary(a, b) {
    let i = a.length - 1, j = b.length - 1, carry = 0;
    const result = [];
    while (i >= 0 || j >= 0 || carry) {
        const sum = (i >= 0 ? +a[i--] : 0) + (j >= 0 ? +b[j--] : 0) + carry;
        result.push(sum % 2);
        carry = Math.floor(sum / 2);
    }
    return result.reverse().join('');
}

atoi: (1) skip leading whitespace, (2) optional sign, (3) digits until non-digit, (4) clamp to 32-bit range. Add binary: process from right, maintain carry, prepend digits. Add strings: same pattern for decimal. Multiply strings: result[i+j] and result[i+j+1] positions for digits at i and j. Process from right to left.

atoi and add binary templates
// String to integer (atoi)
function myAtoi(s) {
    let i = 0, sign = 1, result = 0;
    const MAX = 2147483647, MIN = -2147483648;
    // Skip whitespace
    while (i < s.length && s[i] === ' ') i++;
    // Sign
    if (i < s.length && (s[i] === '+' || s[i] === '-'))
        sign = s[i++] === '-' ? -1 : 1;
    // Digits
    while (i < s.length && s[i] >= '0' && s[i] <= '9') {
        const digit = s[i++] - '0';
        if (result > Math.floor((MAX - digit) / 10)) // overflow check
            return sign === 1 ? MAX : MIN;
        result = result * 10 + digit;
    }
    return sign * result;
}

// Add two binary strings
function addBinary(a, b) {
    let i = a.length - 1, j = b.length - 1, carry = 0;
    const result = [];
    while (i >= 0 || j >= 0 || carry) {
        const sum = (i >= 0 ? +a[i--] : 0) + (j >= 0 ? +b[j--] : 0) + carry;
        result.push(sum % 2);
        carry = Math.floor(sum / 2);
    }
    return result.reverse().join('');
}
atoi edge cases checklist: Leading whitespace, optional sign, overflow (check before multiplying), stop at non-digit, return 0 for empty/no-digit input.

Add binary/strings trick: Process from right with carry. Loop condition: i >= 0 OR j >= 0 OR carry. This handles different lengths and final carry automatically.

Multiply strings: Result length ≤ m+n. For digits at positions i (from right in num1) and j (from right in num2): add to pos[i+j+1] and pos[i+j] for tens carry. Single pass over all digit pairs.