Add binary strings
Add two nonempty strings containing only binary digits. Return a canonical binary string with no leading zeros, except zero itself. Do not convert inputs to integers.
left, right, carry = 1, 1, 1
total = left+right+carry
print(total % 2, total // 2)Reason about the state
Walk backward with carry and emit one result digit per position. Reverse the collected digits after the final carry. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Add two nonempty strings containing only binary digits. Return a canonical binary string with no leading zeros, except zero itself. Do not convert inputs to integers.
The explanation opens here once every check passes.