Given a string S consisting of N lowercase characters, the task is to modify the given string such that no subsequence of length two repeats in the string by removing minimum number of characters.
Examples:
Input: S = “abcaadbcd”
Output: abcd
Explanation: Removing the characters at indices {2, 3, 4, 5, 6, 7} modifies the string to “abcd”, that contains every subsequence of length 2 exactly once.
Input: S = “cadbcc”
Output: cadbc
Approach: The given problem can be solved based on the observation that the final string can contain only unique characters with the exception that the first character can occur 2 times in the string, one at the start and the other at the end(if possible). Follow the steps below to solve the problem:
- Initialize an empty string, say ans to store the resultant final string.
- Initialize a boolean array C[] of size 26 to check if the character is present in the final string or not.
- Initialize a variable, say pos as 0 to store the index of the last character added to the string, ans.
- Traverse the given string S and if the current character is not present in ans, then append it to ans, mark it as visited in the array C[], and update the value of pos to i.
- Iterate over the range [pos + 1, N – 1] using the variable i and if S[i] is equal to S[0], then append it to the final string ans and break out of the loop.
- After completing the above steps, print the string ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void RemoveCharacters(string s)
{
string ans = "" ;
bool c[26];
for ( int i = 0; i < 26; i++)
c[i] = 0;
int pos = 0;
for ( int i = 0; i < s.size(); i++) {
if (c[s[i] - 'a' ] == 0) {
c[s[i] - 'a' ] = 1;
pos = i;
ans += s[i];
}
}
for ( int i = pos + 1;
i < ( int )s.size(); i++) {
if (s[i] == s[0]) {
ans += s[i];
break ;
}
}
cout << ans;
}
int main()
{
string S = "abcaadbcd" ;
RemoveCharacters(S);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void RemoveCharacters(String s)
{
String ans = "" ;
int []c = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++)
c[i] = 0 ;
int pos = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (c[( int )s.charAt(i) - 97 ] == 0 ) {
c[( int )s.charAt(i) - 97 ] = 1 ;
pos = i;
ans += s.charAt(i);
}
}
for ( int i = pos + 1 ;
i < s.length(); i++) {
if (s.charAt(i) == s.charAt( 0 )) {
ans += s.charAt(i);
break ;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
String S = "abcaadbcd" ;
RemoveCharacters(S);
}
}
|
Python3
def RemoveCharacters(s):
ans = ""
c = [ 0 for i in range ( 26 )]
pos = 0
for i in range ( len (s)):
if (c[ ord (s[i]) - 97 ] = = 0 ):
c[ ord (s[i]) - 97 ] = 1
pos = i
ans + = s[i]
for i in range (pos + 1 , len (s), 1 ):
if (s[i] = = s[ 0 ]):
ans + = s[i]
break
print (ans)
if __name__ = = '__main__' :
S = "abcaadbcd"
RemoveCharacters(S)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void RemoveCharacters( string s)
{
string ans = "" ;
int []c = new int [26];
for ( int i = 0; i < 26; i++)
c[i] = 0;
int pos = 0;
for ( int i = 0; i < s.Length; i++) {
if (c[( int )s[i] - 97] == 0) {
c[( int )s[i] - 97] = 1;
pos = i;
ans += s[i];
}
}
for ( int i = pos + 1;
i < s.Length; i++) {
if (s[i] == s[0]) {
ans += s[i];
break ;
}
}
Console.Write(ans);
}
public static void Main()
{
string S = "abcaadbcd" ;
RemoveCharacters(S);
}
}
|
Javascript
<script>
function RemoveCharacters(s)
{
var ans = "" ;
var c = new Array(26);
var i;
for (i = 0; i < 26; i++)
c[i] = 0;
var pos = 0;
for (i = 0; i < s.length; i++) {
if (c[s[i].charCodeAt() - 97] == 0) {
c[s[i].charCodeAt() - 97] = 1;
pos = i;
ans += s[i];
}
}
for (i = pos + 1;
i < s.length; i++) {
if (s[i] == s[0]) {
ans += s[i];
break ;
}
}
document.write(ans);
}
var S = "abcaadbcd" ;
RemoveCharacters(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)