# Check if two same sub-sequences exist in a string or not

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.

Examples:

Input: str = “geeksforgeeks”
Output: YES
Two possible sub-sequences are “geeks” and “geeks”.

Input: str = “bhuvan”
Output: NO

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

## C++

 `// C++ program to Check if ` `// similar subsequences exist or not ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if similar subsequences ` `// occur in a string or not ` `bool` `check(string s, ``int` `l) ` `{ ` ` `  `    ``int` `freq = { 0 }; ` `    ``// iterate and count the frequency ` `    ``for` `(``int` `i = 0; i < l; i++) { ` `        ``freq[s[i] - ``'a'``]++; ``// counting frequency of the letters ` `    ``} ` ` `  `    ``// check if frequency is more ` `    ``// than once of any character ` `    ``for` `(``int` `i = 0; i < 26; i++) { ` `        ``if` `(freq[i] >= 2) ` `            ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` `// Driver Code ` `int` `main() ` `{ ` `    ``string s = ``"geeksforgeeks"``; ` `    ``int` `l = s.length(); ` `    ``if` `(check(s, l)) ` `        ``cout << ``"YES"``; ` `    ``else` `        ``cout << ``"NO"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to Check  ` `// if similar subsequences  ` `// exist or not ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.util.Arrays; ` ` `  `class` `GFG ` `{ ` `// Function to check if ` `// similar subsequences ` `// occur in a string or not ` `static` `boolean` `check(String s,  ` `                     ``int` `l) ` `{ ` `    ``int` `freq[] = ``new` `int``[``26``]; ` `    ``Arrays.fill(freq, ``0``); ` `     `  `    ``// iterate and count ` `    ``// the frequency ` `    ``for` `(``int` `i = ``0``; i < l; i++)  ` `    ``{ ` `        ``// counting frequency ` `        ``// of the letters ` `        ``freq[s.charAt(i) - ``'a'``]++;  ` `    ``} ` ` `  `    ``// check if frequency is more ` `    ``// than once of any character ` `    ``for` `(``int` `i = ``0``; i < ``26``; i++)  ` `    ``{ ` `        ``if` `(freq[i] >= ``2``) ` `            ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``String s = ``"geeksforgeeks"``; ` `    ``int` `l = s.length(); ` `    ``if` `(check(s, l)) ` `        ``System.out.print(``"YES"``); ` `    ``else` `        ``System.out.print(``"NO"``); ` `} ` `} `

## Python3

 `# 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``): ` `            ``return` `True` ` `  `    ``return` `False` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``s ``=` `"geeksforgeeks"` `    ``l ``=` `len``(s) ` `    ``if` `(check(s, l)): ` `        ``print``(``"YES"``) ` `    ``else``: ` `        ``print``(``"NO"``) ` ` `  `# This code is contributed by ` `# Sahil_Shelangia `

## C#

 `// C# Pprogram to Check if similar subsequences  ` `// exist or not ` `using` `System; ` `using` `System.Collections.Generic;              ` ` `  `class` `GFG ` `{ ` `     `  `// Function to check if similar subsequences ` `// occur in a string or not ` `static` `bool` `check(String s, ``int` `l) ` `{ ` `    ``int` `[]freq = ``new` `int``; ` `     `  `    ``// iterate and count the frequency ` `    ``for` `(``int` `i = 0; i < l; i++)  ` `    ``{ ` `        ``// counting frequency of the letters ` `        ``freq[s[i] - ``'a'``]++;  ` `    ``} ` ` `  `    ``// check if frequency is more ` `    ``// than once of any character ` `    ``for` `(``int` `i = 0; i < 26; i++)  ` `    ``{ ` `        ``if` `(freq[i] >= 2) ` `            ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``String s = ``"geeksforgeeks"``; ` `    ``int` `l = s.Length; ` `    ``if` `(check(s, l)) ` `        ``Console.WriteLine(``"YES"``); ` `    ``else` `        ``Console.WriteLine(``"NO"``); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```YES
```

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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.