67. Add Binary | Leetcode 67 | Easy Code Explanation | Simple approach | C++

preview_player
Показать описание

Approach and Explanation

🔢 Problem: Add Binary – LeetCode #67
You're given two binary strings a and b. Your task is to return their sum as a binary string — just like how we do decimal addition with carry, but this time with 0s and 1s!

🧠 Example:
Input:
a = "11"
b = "1"
Output:
"100"
✅ Explanation: 11 (which is 3) + 1 = 100 (which is 4 in binary)

Input:
a = "1010"
b = "1011"
Output:
"10101"
✅ Explanation: 10 + 11 = 21 (in binary = 10101)

🔍 Explanation (In Simple Words):
We add both binary strings from the end, just like how we add numbers from right to left.
We track a carry as we go:
0 + 0 = 0
1 + 0 = 1
1 + 1 = 10 (so write 0 and carry 1)
1 + 1 + carry = 11 (so write 1 and carry 1)
We build the result in reverse, and finally return it as the binary sum string.

⏱️ Time & Space Complexity:
Space Complexity: O(1) extra space (excluding result string)

#AddBinary #BinaryAddition #LeetcodeEasy #CodingShorts
#DSAinCPlusPlus #LogicBuilding #BitManipulation #StringProblems
#InterviewPrep #BinaryMath #LeetcodeSolutions #AddBinary #Leetcode67 #BinaryAddition #DSAinCPP #LeetcodeEasy
#CodingShorts #InterviewPrep #LogicBuilding #BinaryMath #BitManipulation
#CPlusPlus #LeetcodeDailyChallenge #StringProblems #TechWithDKBoss
#Leetcode #TwoPointers #Simulation #TopInterviewQuestions
Рекомендации по теме
join shbcf.ru