Minimum moves to empty a String by repeatedly deleting substrings with different start and end
Given a string str, the task is to find the minimum number of moves required to make str empty by deleting any substring whose starting and ending characters are different. If it is not possible to empty a string, return “-1“.
Examples:
Input: str = “abba”
Output: 2
Explanation: Follow the following steps to get the empty string: “abba” -> (delete ab) -> “ba” -> (delete ba) -> “”
Input: aba
Output: -1
Explanation: It is not possible to empty the given string.
Input: abc
Output: 1
Approach: The task can be solved on the basis of observations.
- If the first & last characters are not equal, then only one operation is needed
- Else, check if there exist any 2 indices, which don’t match with the first & last characters. If they exist, the minimum number of operations needed are two, else it is impossible to reduce the string to an empty string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numberOfMoves(string& str, int n)
{
if (str[0] != str[n - 1]) {
cout << "1" ;
return ;
}
for ( int i = 0; i < n - 1; i++) {
if (str[i] != str[0]
&& str[i + 1] != str[n - 1]) {
cout << "2" ;
return ;
}
}
cout << -1;
return ;
}
int main()
{
string str = "abba" ;
int n = 4;
numberOfMoves(str, n);
}
|
Java
import java.io.*;
class GFG
{
static void numberOfMoves(String str, int n)
{
if (str.charAt( 0 ) != str.charAt(n- 1 )) {
System.out.println( "1" );
return ;
}
for ( int i = 0 ; i < n - 1 ; i++) {
if (str.charAt(i) != str.charAt( 0 )
&& str.charAt(i+ 1 ) != str.charAt(n- 1 )) {
System.out.println( "2" );
return ;
}
}
System.out.println(- 1 );
return ;
}
public static void main (String[] args) {
String str = "abba" ;
int n = 4 ;
numberOfMoves(str, n);
}
}
|
Python3
def numberOfMoves( str , n):
if ( str [ 0 ] ! = str [n - 1 ]):
print ( "1" )
return
for i in range ( 0 , len ( str )):
if ( str [i] ! = str [ 0 ] and
str [i + 1 ] ! = str [n - 1 ]):
print ( "2" )
return
print ( - 1 )
return
if __name__ = = '__main__' :
str = "abba"
n = 4 ;
numberOfMoves( str , n);
|
C#
using System;
public class GFG
{
static void numberOfMoves( string str, int n)
{
if (str[0] != str[n - 1]) {
Console.WriteLine( "1" );
return ;
}
for ( int i = 0; i < n - 1; i++) {
if (str[i] != str[0]
&& str[i + 1] != str[n - 1]) {
Console.WriteLine( "2" );
return ;
}
}
Console.WriteLine(-1);
return ;
}
public static void Main(String []args) {
string str = "abba" ;
int n = 4;
numberOfMoves(str, n);
}
}
|
Javascript
<script>
function numberOfMoves(str, n) {
if (str[0] != str[n - 1]) {
document.write( "1" );
return ;
}
for (let i = 0; i < n - 1; i++) {
if (str[i] != str[0]
&& str[i + 1] != str[n - 1]) {
document.write( "2" );
return ;
}
}
document.write(-1);
return ;
}
let str = "abba" ;
let n = 4;
numberOfMoves(str, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1).
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...