Repeated subsequence of length 2 or more
Given a string, find if there is any subsequence of length 2 or more that repeats itself such that the two subsequences don’t have the same character at the same position, i.e., any 0’th or 1st character in the two subsequences shouldn’t have the same index in the original string.
Example:
Input: ABCABD
Output: Repeated Subsequence Exists (A B is repeated)
Input: ABBB
Output: Repeated Subsequence Exists (B B is repeated)
Input: AAB
Output: Repeated Subsequence Doesn't Exist (Note that
A B cannot be considered as repeating because B is at
same position in two subsequences).
Input: AABBC
Output: Repeated Subsequence Exists (A B is repeated)
Input: ABCDACB
Output: Repeated Subsequence Exists (A B is repeated)
Input: ABCD
Output: Repeated Subsequence Doesn't Exist
The problem is a classic variation of longest common subsequence problem. We have discussed Dynamic programming solution here. Dynamic programming solution takes O(n2) time and space.
In this post, O(n) time and space approach is discussed.
The idea is to remove all the non-repeated characters from the string and check if the resultant string is palindrome or not. If the remaining string is palindrome then it is not repeated, else there is a repetition. One special case we need to handle for inputs like “AAA”, which are palindrome but their repeated subsequence exists. Repeated subsequence exists for a palindrome string if it is of odd length and its middle letter is the same as the left(or right) character.
Algorithm:
- Step 1: Initialize the input string.
- Step 2: Find the length of input string
- Step 3: Create an array and store all characters and their frequencies in it.
- Step 4: Traverse the input string and store the frequency of all characters in another array.
- Step 5: increment the count if the letters are repeating, if count is more than two return true.
- Step 6: In place of non repeating characters put ‘\0’ .
- Step 7: check if the string is palindrome, if it’s palindrome return false else true.
- Step 7: Print the output.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
#define MAX_CHAR 256
using namespace std;
bool isPalindrome( char str[], int l, int h)
{
while (h > l)
if (str[l++] != str[h--])
return false ;
return true ;
}
int check( char str[])
{
int n = strlen (str);
int freq[MAX_CHAR] = { 0 };
for ( int i = 0; i < n; i++)
{
freq[str[i]]++;
if (freq[str[i]] > 2)
return true ;
}
int k = 0;
for ( int i = 0; i < n; i++)
if (freq[str[i]] > 1)
str[k++] = str[i];
str[k] = '\0' ;
if (isPalindrome(str, 0, k-1))
{
if (k & 1)
return str[k/2] == str[k/2 - 1];
return false ;
}
return true ;
}
int main()
{
char str[] = "ABCABD" ;
if (check(str))
cout << "Repeated Subsequence Exists" ;
else
cout << "Repeated Subsequence Doesn't Exists" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX_CHAR = 256 ;
public static boolean isPalindrome(String str, int l,
int h)
{
while (h > l)
if (str.charAt(l++) != str.charAt(h--))
return false ;
return true ;
}
public static boolean check(String str)
{
int n = str.length();
int [] freq = new int [MAX_CHAR];
for ( int i = 0 ; i < n; i++) {
freq[str.charAt(i)]++;
if (freq[str.charAt(i)] > 2 )
return true ;
}
int k = 0 ;
for ( int i = 0 ; i < n; i++)
if (freq[str.charAt(i)] > 1 )
str.replace(str.charAt(k++), str.charAt(i));
str.replace(str.charAt(k), '\0' );
if (isPalindrome(str, 0 , k - 1 )) {
if ((k & 1 ) == 1 ) {
if (k / 2 >= 1 )
return (str.charAt(k / 2 )
== str.charAt(k / 2 - 1 ));
}
return false ;
}
return true ;
}
public static void main(String[] args)
{
String str = "ABCABD" ;
if (check(str))
System.out.println(
"Repeated Subsequence Exists" );
else
System.out.println( "Repeated Subsequence"
+ " Doesn't Exists" );
}
}
|
Python3
MAX_CHAR = 256
def isPalindrome( Str , l, h):
while (h > l):
if ( Str [l] ! = Str [h]):
l + = 1
h - = 1
return False
return True
def check( Str ):
n = len ( Str )
freq = [ 0 for i in range (MAX_CHAR)]
for i in range (n):
freq[ ord ( Str [i])] + = 1
if (freq[ ord ( Str [i])] > 2 ):
return True
k = 0
for i in range (n):
if (freq[ ord ( Str [i])] > 1 ):
Str [k] = Str [i]
k + = 1
Str [k] = '\0'
if (isPalindrome( Str , 0 , k - 1 )):
if (k & 1 ):
return Str [k / / 2 ] = = Str [k / / 2 - 1 ]
return False
return True
S = "ABCABD"
Str = [i for i in S]
if (check( Str )):
print ( "Repeated Subsequence Exists" )
else :
print ( "Repeated Subsequence Doesn't Exists" )
|
C#
using System;
class GFG
{
static int MAX_CHAR = 256;
public static Boolean isPalindrome(String str,
int l, int h)
{
while (h > l)
if (str[l++] != str[h--])
return false ;
return true ;
}
public static Boolean check(String str)
{
int n = str.Length;
int [] freq = new int [MAX_CHAR];
for ( int i = 0; i < n; i++)
{
freq[str[i]]++;
if (freq[str[i]] > 2)
return true ;
}
int k = 0;
for ( int i = 0; i < n; i++)
if (freq[str[i]] > 1)
str.Replace(str[k++],
str[i]);
str.Replace(str[k], '\0' );
if (isPalindrome(str, 0, k - 1))
{
if ((k & 1) == 1)
{
if (k / 2 >= 1)
return (str[k / 2] ==
str[k / 2 - 1]);
}
return false ;
}
return true ;
}
public static void Main(String[] args)
{
String str = "ABCABD" ;
if (check(str))
Console.WriteLine( "Repeated Subsequence Exists" );
else
Console.WriteLine( "Repeated Subsequence" +
" Doesn't Exists" );
}
}
|
Javascript
<script>
let MAX_CHAR = 256;
function isPalindrome(str,l,h)
{
while (h > l)
if (str[l++] != str[h--])
return false ;
return true ;
}
function check(str)
{
let n = str.length;
let freq = new Array(MAX_CHAR);
for (let i=0;i<freq.length;i++)
{
freq[i]=0;
}
for (let i = 0; i < n; i++)
{
freq[str[i].charCodeAt(0)]++;
if (freq[str[i].charCodeAt(0)] > 2)
return true ;
}
let k = 0;
for (let i = 0; i < n; i++)
if (freq[str[i].charCodeAt(0)] > 1)
str.replace(str[k++],
str[i]);
str.replace(str[k], '\0' );
if (isPalindrome(str, 0, k - 1))
{
if ((k & 1) == 1)
{
if (k / 2 >= 1)
return (str[(Math.floor(k/2))] ==
str[(Math.floor(k/2) -1)]);
}
return false ;
}
return true ;
}
let str = "ABCABD" ;
if (check(str))
document.write( "Repeated Subsequence Exists" );
else
document.write( "Repeated Subsequence" +
" Doesn't Exists" );
</script>
|
Output
Repeated Subsequence Exists
Time Complexity: O(n).
Auxiliary Space: O(256), because we are using frequency array size of MAX_CHAR.
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...