---
# System prepended metadata

title: 0415. Add Strings
tags: [Leetcode, Add Simulation, FaceBook, Easy]

---

# 0415. Add Strings
###### tags: `Leetcode` `Easy` `FaceBook` `Add Simulation`
Link: https://leetcode.com/problems/add-strings/
## Code O(N) O(1)
```java=
class Solution {
    public String addStrings(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        if(len1<len2) return addStrings(num2, num1);
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        for(int i = 0;i < num2.length();i++){
            int a = num1.charAt(num1.length()-i-1)-'0';
            int b = num2.charAt(num2.length()-i-1)-'0';
            sb.append((a+b+carry)%10);
            carry = (a+b+carry)/10;
        }
        for(int i = num1.length()-num2.length()-1;i >= 0;i--){
            int a = num1.charAt(i)-'0';
            sb.append((a+carry)%10);
            carry = (a+carry)/10;
        }
        if(carry == 1) sb.append(1);
        return sb.reverse().toString();
    }
}
```