Given a string str, the task is to make the string start and end at the same character with the minimum number of given operations. In a single operation, any character of the string can be removed. Note that the length of the resultant string must be greater than 1 and it is not possible then print -1.
Examples:
Input: str = “geeksforgeeks”
Output: 3
Remove the first and the last two characters
and the string becomes “eeksforgee”
Input: str = “abcda”
Output: 0
Approach: If the string needs to start and end at a character say ch then an optimal way is to remove all the characters before the first occurrence of ch and all the characters after the last occurrence of ch. Find the number of characters that need to be removed for every possible value of ch starting from ‘a’ to ‘z’ and choose the one with the minimum number of delete operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
int minOperation(string str, int len)
{
int first[MAX], last[MAX];
for ( int i = 0; i < MAX; i++) {
first[i] = -1;
last[i] = -1;
}
for ( int i = 0; i < len; i++) {
int index = (str[i] - 'a' );
if (first[index] == -1)
first[index] = i;
last[index] = i;
}
int minOp = -1;
for ( int i = 0; i < MAX; i++) {
if (first[i] == -1 || first[i] == last[i])
continue ;
int cnt = len - (last[i] - first[i] + 1);
if (minOp == -1 || cnt < minOp)
minOp = cnt;
}
return minOp;
}
int main()
{
string str = "abcda" ;
int len = str.length();
cout << minOperation(str, len);
return 0;
}
|
Java
class GFG
{
final static int MAX = 26 ;
static int minOperation(String str, int len)
{
int first[] = new int [MAX];
int last[] = new int [MAX];
for ( int i = 0 ; i < MAX; i++)
{
first[i] = - 1 ;
last[i] = - 1 ;
}
for ( int i = 0 ; i < len; i++)
{
int index = (str.charAt(i) - 'a' );
if (first[index] == - 1 )
first[index] = i;
last[index] = i;
}
int minOp = - 1 ;
for ( int i = 0 ; i < MAX; i++)
{
if (first[i] == - 1 ||
first[i] == last[i])
continue ;
int cnt = len - (last[i] - first[i] + 1 );
if (minOp == - 1 || cnt < minOp)
minOp = cnt;
}
return minOp;
}
public static void main (String[] args)
{
String str = "abcda" ;
int len = str.length();
System.out.println(minOperation(str, len));
}
}
|
Python3
MAX = 26 ;
def minOperation( str , len ):
first, last = [ 0 ] * MAX , [ 0 ] * MAX ;
for i in range ( MAX ):
first[i] = - 1 ;
last[i] = - 1 ;
for i in range ( len ):
index = ( ord ( str [i]) - ord ( 'a' ));
if (first[index] = = - 1 ):
first[index] = i;
last[index] = i;
minOp = - 1 ;
for i in range ( MAX ):
if (first[i] = = - 1 or first[i] = = last[i]):
continue ;
cnt = len - (last[i] - first[i] + 1 );
if (minOp = = - 1 or cnt < minOp):
minOp = cnt;
return minOp;
str = "abcda" ;
len = len ( str );
print ( minOperation( str , len ));
|
C#
using System;
class GFG
{
readonly static int MAX = 26;
static int minOperation(String str, int len)
{
int []first = new int [MAX];
int []last = new int [MAX];
for ( int i = 0; i < MAX; i++)
{
first[i] = -1;
last[i] = -1;
}
for ( int i = 0; i < len; i++)
{
int index = (str[i] - 'a' );
if (first[index] == -1)
first[index] = i;
last[index] = i;
}
int minOp = -1;
for ( int i = 0; i < MAX; i++)
{
if (first[i] == -1 ||
first[i] == last[i])
continue ;
int cnt = len - (last[i] - first[i] + 1);
if (minOp == -1 || cnt < minOp)
minOp = cnt;
}
return minOp;
}
public static void Main (String[] args)
{
String str = "abcda" ;
int len = str.Length;
Console.WriteLine(minOperation(str, len));
}
}
|
Javascript
<script>
var MAX = 26;
function minOperation(str, len)
{
var first = Array(MAX).fill(-1);
var last = Array(MAX).fill(-1);
for ( var i = 0; i < len; i++) {
var index = (str[i].charCodeAt(0) - 'a' .charCodeAt(0));
if (first[index] == -1)
first[index] = i;
last[index] = i;
}
var minOp = -1;
for ( var i = 0; i < MAX; i++) {
if (first[i] == -1 || first[i] == last[i])
continue ;
var cnt = len - (last[i] - first[i] + 1);
if (minOp == -1 || cnt < minOp)
minOp = cnt;
}
return minOp;
}
var str = "abcda" ;
var len = str.length;
document.write( minOperation(str, len));
</script>
|
Time Complexity: O(len), where len is length of string.
Auxiliary Space : O(1)