Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
Last Updated :
08 Dec, 2022
Given a string S and two integers N and K, the task is to find the maximum length suffix such that the occurrence of each character in the suffix string is less than N, and at most K elements can be deleted from the input string to get maximum length suffix.
Examples:
Input: S = “iahagafedcba”, N = 1, K = 0
Output: fedcba
Explanation:
Maximum length suffix in the given string
Such that occurrence of each character is 1 is “fedcba”,
Because if we take the string “afedcba”,
then the occurrence of character “a” will be 2.
Input: S = “iahagafedcba”, N = 1, K = 2
Output: hgfedcba
Explanation:
Maximum length suffix in the given string
Such that occurrence of each character is 1 is “hgfedcba”,
After deleting character “a” two times.
Approach: The idea is to use hash-map to store the frequency of the characters of the string.
- Initialize an empty string to store the longest suffix of the string.
- Iterate the string from last using a loop –
- Increment the occurrence of the character by 1 in the hash-map
- If the occurrence of current character is less than N, then add the character into the suffix string
- Otherwise, decrement the value of K by 1 if the value of K is greater than 0
- Print the suffix string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumSuffix(string s,
int n, int k){
int i = s.length() - 1;
int arr[26] = { 0 };
string suffix = "" ;
while (i > -1) {
int index = s[i] - 'a' ;
if (arr[index] < n) {
arr[index]++;
suffix += s[i];
i--;
continue ;
}
if (k == 0)
break ;
k--;
i--;
}
reverse(suffix.begin(), suffix.end());
cout << suffix;
}
int main()
{
string str = "iahagafedcba" ;
int n = 1, k = 2;
maximumSuffix(str, n, k);
return 0;
}
|
Java
public class GFG{
static void maximumSuffix(String s,
int n, int k){
int i = s.length() - 1 ;
int arr[] = new int [ 26 ];
String suffix = "" ;
while (i > - 1 ) {
int index = s.charAt(i) - 'a' ;
if (arr[index] < n) {
arr[index]++;
suffix += s.charAt(i);
i--;
continue ;
}
if (k == 0 )
break ;
k--;
i--;
}
suffix = reverse(suffix);
System.out.print(suffix);
}
static String reverse(String input) {
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String str = "iahagafedcba" ;
int n = 1 , k = 2 ;
maximumSuffix(str, n, k);
}
}
|
Python 3
def maximumSuffix(s, n, k):
i = len (s) - 1
arr = [ 0 for i in range ( 26 )]
suffix = ""
while (i > - 1 ):
index = ord (s[i]) - ord ( 'a' );
if (arr[index] < n):
arr[index] + = 1
suffix + = s[i]
i - = 1
continue
if (k = = 0 ):
break
k - = 1
i - = 1
suffix = suffix[:: - 1 ]
print (suffix)
if __name__ = = '__main__' :
str = "iahagafedcba"
n = 1
k = 2
maximumSuffix( str , n, k)
|
C#
using System;
class GFG{
static void maximumSuffix(String s,
int n, int k){
int i = s.Length - 1;
int []arr = new int [26];
String suffix = "" ;
while (i > -1) {
int index = s[i] - 'a' ;
if (arr[index] < n) {
arr[index]++;
suffix += s[i];
i--;
continue ;
}
if (k == 0)
break ;
k--;
i--;
}
suffix = reverse(suffix);
Console.Write(suffix);
}
static String reverse(String input) {
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
public static void Main(String[] args)
{
String str = "iahagafedcba" ;
int n = 1, k = 2;
maximumSuffix(str, n, k);
}
}
|
Javascript
<script>
function maximumSuffix(s,n, k){
let i = s.length - 1;
let arr = new Array(26).fill(0);
let suffix = "" ;
while (i > -1) {
let index = s.charCodeAt(i) - 97;
if (arr[index] < n) {
arr[index]++;
suffix += s[i];
i--;
continue ;
}
if (k == 0)
break ;
k--;
i--;
}
suffix = suffix.split( "" ).reverse().join( "" );
document.write(suffix);
}
let str = "iahagafedcba" ;
let n = 1, k = 2;
maximumSuffix(str, n, k);
</script>
|
Performance Analysis:
- Time Complexity: In the above-given approach, there is one loop for iterating over string which takes O(L) time in worst case. Therefore, the time complexity for this approach will be O(L).
- Auxiliary Space Complexity: In the above-given approach, there is extra space used to store the frequency of the character. Therefore, the auxiliary space complexity for the above approach will be O(L)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...