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.
# Python 3 program to Check if
# similar subsequences exist or not
# Function to check if similar subsequences
# occur in a string or not
def check(s, l):
freq = [0 for i in range(26)]
# iterate and count the frequency
for i in range(l):
# counting frequency of the letters
freq[ord(s[i]) – ord(‘a’)] += 1
# check if frequency is more
# than once of any character
for i in range(26):
if (freq[i] >= 2):
# Driver Code
if __name__ == ‘__main__’:
s = “geeksforgeeks”
l = len(s)
if (check(s, l)):
# This code is contributed by
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.
- Count subsequences in first string which are anagrams of the second string
- Print all subsequences of a string
- Count of 'GFG' Subsequences in the given string
- Print all subsequences of a string using ArrayList
- Number of subsequences in a string divisible by n
- Number of subsequences as "ab" in a string repeated K times
- Print all subsequences of a string | Iterative Method
- Lexicographically smallest permutation of a string with given subsequences
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Number of ways to partition a string into two balanced subsequences
- Generating distinct subsequences of a given string in lexicographic order
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- String slicing in Python to check if a string can become empty by recursive deletion
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.
Improved By : sahilshelangia