Algorithm:
- Calculate the sum of the ASCII codes of all the characters in the first string,
s
. - Calculate the sum of the ASCII codes of all the characters in the second string,
t
. - Subtract the sum of the ASCII codes of
s
from the sum of the ASCII codes oft
. - Return the character represented by the difference.
Time complexity: O(n), where n is the length of the longer string.
Space complexity: O(1), as we only use a few constant variables.
for example, consider the following two strings:
s = "ABCDELMN" t = "ABCFGLMN"
The sum of the ASCII codes of all the characters in s
is:
A + B + C + D + E + L + M + N = 65 + 66 + 67 + 68 + 69 + 76 + 77 + 78 = 536
The sum of the ASCII codes of all the characters in t
is:
A + B + C + F + G + L + M + N = 65 + 66 + 67 + 70 + 71 + 76 + 77 + 78 = 540
The difference between the sum of the ASCII codes of t
and s
is 540 - 536 = 4
The character represented by 4 is F
. Therefore, the function will return F
.