Given a string, the task is to check if there exist two equal sub-sequences in the given string. Two sub-sequences are said to be equal if they have the same characters arranged in the same lexicographical order but the position of characters differs from that in the original string.
Input: str = “geeksforgeeks”
Two possible sub-sequences are “geeks” and “geeks”.
Input: str = “bhuvan”
Approach: The approach to solve this problem is to check if any character appears more than once. Since the minimal length of matching subsequence can be 1, hence if a character occurrence in a string more than once then two similar subsequences is possible. Initialize a freq array of length 26. Iterate over the string and increment the characters frequency. Iterate over the freq array and check if freq[i] for any i in range of 0-26 is more than 1, then it is possible.
Below is the implementation of the above approach.
Time Complexity: O(N)
Auxiliary Space: O(1)
Note: If the length of similar subsequence was mentioned, then the approach to solve the problem will also be different. The approach to check if a string contains two repeated subsequences of length two or more is discussed in this post.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize product of same-indexed elements of same size subsequences
- Check whether two strings contain same characters in same order
- Check if a string can be obtained by appending subsequences of another string
- Check if string S2 can be obtained by appending subsequences of string S1
- Check if an array can be split into 3 subsequences of equal sum or not
- Program to check if two strings are same or not
- Count subsequences with same values of Bitwise AND, OR and XOR
- Count of subsequences consisting of the same element
- Check if a binary string contains consecutive same or not
- Number of ways to partition a string into two balanced subsequences
- Loss when two items are sold at same price and same percentage profit/loss
- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- Maximize subsequences having array elements not exceeding length of the subsequence
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if a string can be split into two strings with same number of K-frequent characters
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Powers of two and subsequences
- Find all combinations of two equal sum subsequences
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.