Last Updated: 25 Dec, 2020

Added Character

Easy
Asked in companies
Red HatCodenation

Problem statement

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'.
Input format:
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.
Constraints:
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.

Approaches

01 Approach

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:

 

  • Iterate through the elements of string ‘T’ (with loop variable ‘i’).
  • If ‘T[i]’ == ‘S[i]’, then move forward.
  • If ‘T[i]’ != ‘S[i]’, then we get the added element and return ‘T[i]’.
  • If ‘T[i]’ == ‘S[i]’, for all ‘i’ less than the length of string ‘S’, then the last character of string ‘T’ will be the required character.

 

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.