Longest subsequence where each character occurs at least k times
Last Updated :
12 Dec, 2022
Given a string ‘s’ and an integer k, find other string ‘t’ such that ‘t’ is the largest subsequence of given string ‘s’ and each character of ‘t’ must occur at least k times in string s.
Examples :
Input : s = "geeksforgeeks"
k = 2
Output : geeksgeeks
Input : s = "baaabaacba"
k = 3
Output : baaabaaba
A simple solution is to generate all subsequences. For every subsequence, check if it has all characters at least k times. Find the longest such subsequence. The time complexity of this approach is exponential.
Efficient Approach We can take another array to keep the record of the count of each character from string s, if any character occurred more than or equal to k times, then we simply print it.
Implementation:
CPP
#include <iostream>
using namespace std;
#define MAX_CHAR 26
void findSubsequence(string str, int k)
{
int a[MAX_CHAR] = { 0 };
for ( int i = 0; i < str.size(); i++)
a[str[i] - 'a' ]++;
for ( int i = 0; i < str.size(); i++)
if (a[str[i] - 'a' ] >= k)
cout << str[i];
}
int main()
{
int k = 2;
findSubsequence( "geeksforgeeks" , k);
return 0;
}
|
Java
class GFG {
static final int MAX_CHAR = 26 ;
static void findSubsequence(String str, int k)
{
int a[] = new int [MAX_CHAR];
for ( int i = 0 ; i < str.length(); i++)
a[str.charAt(i) - 'a' ]++;
for ( int i = 0 ; i < str.length(); i++)
if (a[str.charAt(i) - 'a' ] >= k)
System.out.print(str.charAt(i));
}
public static void main(String[] args) {
int k = 2 ;
findSubsequence( "geeksforgeeks" , k);
}
}
|
Python3
MAX_CHAR = 26
def findSubsequence(stri, k):
a = [ 0 ] * MAX_CHAR;
for i in range ( len (stri)):
a[ ord (stri[i]) - ord ( 'a' )] + = 1
for i in range ( len (stri)):
if a[ ord (stri[i]) - ord ( 'a' )] > = k:
print (stri[i],end = '')
k = 2
findSubsequence( "geeksforgeeks" , k)
|
C#
using System;
class GFG {
static int MAX_CHAR = 26;
static void findSubsequence( string str, int k)
{
int []a = new int [MAX_CHAR];
for ( int i = 0; i < str.Length; i++)
a[str[i] - 'a' ]++;
for ( int i = 0; i < str.Length; i++)
if (a[str[i] - 'a' ] >= k)
Console.Write(str[i]);
}
public static void Main() {
int k = 2;
findSubsequence( "geeksforgeeks" , k);
}
}
|
PHP
<?php
function findSubsequence( $str , $k )
{
$a = array (1024);
for ( $i = 0; $i < 26; $i ++)
$a [ $i ] = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
$temp = ord( $str [ $i ]) - ord( 'a' );
$a [ $temp ] += 1;
}
for ( $i = 0; $i < strlen ( $str ); $i ++)
if ( $a [ord( $str [ $i ]) - ord( 'a' )] >= $k )
echo $str [ $i ];
}
$k = 2;
findSubsequence( "geeksforgeeks" , $k );
?>
|
Javascript
<script>
var MAX_CHAR = 26;
function findSubsequence(str, k)
{
var a = Array(MAX_CHAR).fill(0);
for ( var i = 0; i < str.length; i++)
a[str[i].charCodeAt(0) -
'a' .charCodeAt(0)]++;
for ( var i = 0; i < str.length; i++)
if (a[str[i].charCodeAt(0) -
'a' .charCodeAt(0)] >= k)
document.write( str[i]);
}
var k = 2;
findSubsequence( "geeksforgeeks" , k);
</script>
|
Time Complexity: O(n), where n is the size of the given string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...