0415. Add Strings

Easy | String + Math | 28 ms (97.36%), 14.2 MB (83.13%)

Source: LeetCode - Add Strings GitHub: Solution / Performance

Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

Sum each character reversely by index indicators starting from the end.

Since two strings might have different lengths, we need to take care of the condition when either one of strings meets the beginning first.

class Solution:
    def addStrings(self, num1: str, num2: str) -> str:
        
        # ==================================================
        #  String + Math                                   =
        # ==================================================
        # time  : O(max(m,n))
        # space : O(1)
        
        ans, carry = '', 0
        
        p1, p2 = len(num1) - 1, len(num2) - 1
        while(p1 >= 0 or p2 >= 0 or carry > 0):
            val1 = ord(num1[p1]) - ord('0') if p1 >= 0 else 0
            val2 = ord(num2[p2]) - ord('0') if p2 >= 0 else 0
            val = val1 + val2 + carry
            carry = val // 10
            val = val % 10
            
            ans = str(val) + ans
            p1 -= 1
            p2 -= 1
            
        return ans

Last updated