-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_numeric_string.cpp
60 lines (51 loc) · 1.64 KB
/
add_numeric_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ADDITION OF LARGE NUMBERS REPRESENTED AS STRINGS
*
* Given two large positive numbers represented as strings, add them together and
* return their sum, also as a string. Numbers are too large to fit into standard integer types.
*
* Constraints:
* - Each string represents a non-negative integer.
* - Each string length can be from 1 to 10^5.
* - Strings will contain digits only (0-9).
*
* Example Input/Output:
* Input: num1 = "999", num2 = "3"
* Output: "1002"
* Explanation: 999 + 3 = 1002.
*/
#include <algorithm>
#include <cassert>
#include <string>
#include <iostream>
// Helper function to check if a string contains only digits
bool isDigitString(const std::string& s) {
return std::all_of(s.begin(), s.end(), ::isdigit);
}
// Optimal Solution
// Adds two numbers represented as strings, O(n) complexity
std::string optimalSolution(const std::string& num1, const std::string& num2) {
std::string result;
int carry = 0, i = num1.size() - 1, j = num2.size() - 1;
while (i >= 0 || j >= 0 || carry) {
int digit1 = (i >= 0) ? num1[i--] - '0' : 0;
int digit2 = (j >= 0) ? num2[j--] - '0' : 0;
int sum = digit1 + digit2 + carry;
carry = sum / 10;
result.push_back((sum % 10) + '0');
}
std::reverse(result.begin(), result.end());
return result;
}
// Test cases for correctness
void test() {
assert(optimalSolution("999", "3") == "1002");
assert(optimalSolution("33", "9999") == "10032");
assert(optimalSolution("3333333", "222") == "3333555");
assert(optimalSolution("0", "0") == "0");
std::cout << "All tests passed!\n";
}
int main() {
test();
return 0;
}