Hi all!
I got this question in code-signal but somehow I was getting TLE on one hidden test case. I am not sure what's wrong in my code. Either I am missing something silly or there's some tiny optimization that I am missing. Any inputs would be appreciated.
Question: Given two strings a and b we need to add ith elements from both. Time limit 0.5secs (AFAIR). (Input size limits I do not remember)
Eg1:
a = "99"
b = "99"
ans = "1818"
Eg2:
a = "9"
b = "11"
ans = "110"
My code
int i = a.size()-1, j = b.size()-1;
string ans = "";
while( i >= 0 && j >= 0){
int sum = (int) (a[i--] - '0') + (int) (b[j--] - '0');
ans = to_string(sum) + ans;
}
if(i >= 0){
ans = a.substring(0,i+1) + ans;
} else if(j >= 0) {
ans = b.substring(0,j+1) + ans;
}
return ans;