You are given a string 'S'. Now one more additional character is introduced in this string, which turns 'S' into new string 'T'. You have to find out this newly added character.
Note:
1. All the characters in string 'S' and 'T' consist of uppercase English alphabets.
2. Length of string 'T' is always 1 more than the length of string 'S'.
The first line of the input contains an integer 'TEST' denoting the number of test cases.
The first line of each test case consists of a string 'S'.
The second line of each test consists of a string 'T'.
Output format:
For each test case, print a character that denotes the newly added character in a new line.
Print the output for each test case in a separate line.
Note:
You don't have to print anything, It has already been taken care of. Just Implement the given function.
1 <= TEST <= 100
1 <= |S|, |T| <= 10^4
Where '|S|' and '|T|' denotes the length of he strings 'S' and 'T' respectively.
Time Limit: 1 sec.
2
APPLE
APPHLE
CODE
CODER
H
R
In test case 1, As only character which is present in “APPHLE” but didn’t present in “APPLE” is ‘H’.
In test case 2, As only character which is present in “CODER” but didn’t present in “CODE" is ‘R’.
2
MANGO
MANNGO
NINJA
NIINJA
N
I
In test case 1, As “MANGO” contains only 1 occurrence of ‘N’ but “MANNGO” contains 2 occurrences of ‘N’.
In test case 2, As “NINJA” contains only 1 occurrence of ‘I’ but “NIINJA" contains 2 occurrences of ‘I’.
Simply iterate through strings and compare.
Here in this algorithm we just need to check for the different character at the particular index. If all indexes has same character in both the strings then return the last character of the string ‘T’ as it is the added character in the string.
The steps are as follows:
Eg. In “APPLE” and “APPHLE”, starting from i=0 till i=2, all characters are the same, but at i=3 ‘L’ and ‘H’ are mismatching characters. Hence ‘H’ which is present in the second string is the added character.
O(N), Where ‘N’ is the length of the given string ‘S’.
Since we are iterating through the string ‘S’ once. Thus the timecomplexity will be O(N).
O(1)
Since as we are using constant extra space. Thus the space complexity will be O(1).