Given two strings validate the output string

Objective: Given two input strings and one output strings, validate if output string contains all the characters from both the input strings.

Example:

Input1 = "boy", Input2 = "girl", output = "gboilry"
Result = True

Input1 = "tutorial", Input2 = "horizon", output = "tuhotoririzoialn"
Result = True

Input1 = "boy", Input2 = "girl", output = "bogiry"
Result = False

Approach:

  • Create a one Hash Map using both the input strings with character as key and its count as value.
  • Iterate through output string and check the each character in map and if exist then reduce its count and if does not exist then return false.
  • At the end iterate through map and check the count of each character, if any of the count is not 0 then return false.
  • If none of the previous steps returned false, return true.

Code:

Output:

input1: boy input2: girl Output String: gboilry
true
input1: boy input2: girl Output String: gboily
false

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: