VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-given-string-can-be-generated-after-concatenating-given-strings/

⇱ Check whether given string can be generated after concatenating given strings - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether given string can be generated after concatenating given strings

Last Updated : 12 Oct, 2022

Given three strings str, A and B. The task is to check whether str = A + B or str = B + A where + denotes concatenation. 
Examples: 
 

Input: str = "GeeksforGeeks", A = "Geeksfo", B = "rGeeks" 
Output: Yes 
str = A + B = "Geeksfo" + "rGeeks" = "GeeksforGeeks"
Input: str = "Delhicapitals", B = "Delmi", C = "capitals" 
Output: No 
 


 


Approach: 
 

  1. If len(str) != len(A) + len(B) then it is not possible to generate str by concatenating a + b or b + a.
  2. Else check whether str starts with a and ends with b or it starts with b and ends with a. Print Yes if any of these is true else print No


Below is the implementation of the above approach:
 


Output: 
Yes

 

Time Complexity: O(max(a,b,c)), as we are using a loop to traverse a, b, and c times. Where a, b, and c are the length of the strings.

Auxiliary Space: O(1), as we are not using any extra space.

Comment
Article Tags:
Article Tags: