VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-two-strings-contain-same-characters-in-same-order/

⇱ Check whether two strings contain same characters in same order - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether two strings contain same characters in same order

Last Updated : 19 Dec, 2022

Given two strings s1 and s2, the task is to find whether the two strings contain the same characters that occur in the same order. For example string "Geeks" and string "Geks" contain the same characters in same order.

Examples: 

Input: s1 = "Geeks", s2 = "Geks" 
Output: Yes

Input: s1 = "Arnab", s2 = "Andrew" 
Output: No 

Approach: We have two strings now we have to check whether the strings contain the same characters in the same order. So we will replace the contiguous similar element with a single element i.e. if we have "eee", we will replace it with a single "e". Now we will check that both the strings are equal or not. If equal then print Yes else No

Below is the implementation of the above approach: 


Output: 
Yes

 

Time Complexity: O(m + n)
Auxiliary Space: O(m + n), where m and n are the length of the given strings s1 and s2 respectively.

Using Recursion


Output
Yes

Time complexity: O(max(M, N)) for strings of length M and N respectively.
Auxiliary Space: O(max(M, N)), due to recursive call stacks.

Comment
Article Tags: