![]() |
VOOZH | about |
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: YesInput: 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:
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
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.