This post is completed by 1 user

  • 0
Add to List
Beginner

344. 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.

Output:

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