GeeksforGeeks App
Open App
Browser
Continue

# Minimize length of a string by removing suffixes and prefixes of same characters

Given a string S of length N consisting only of characters ‘a’, ‘b’, and ‘c’, the task is to minimize the length of the given string by performing the following operations only once:

• Divide the string into two non-empty substrings and then, append the left substring to the end of the right substring.
• While appending, if the suffix of the right substring and prefix of the left substring contains the same character, then remove those characters from the suffix and prefix of the right and left substrings respectively. Repeat this step until no substrings are non-removable.

Examples:

Input: S = “aabcccabba”
Output: 4
Explanation:
Divide the given string S in two parts as “aabcc” and “cabba”. Below are the operations performed on the above two substrings:

• Remove the prefixes and suffixes of the same characters, i.e. ‘a’. The string becomes “bcc” and “cabb”.
• Remove the suffixes and prefixes of the same characters, i.e. ‘b’. The string becomes “cc” and “ca”.

Now, after concatenating the right and left substrings, the string obtained is “cacc”, which is of the shortest length, i.e. 4.

Input: S = “aacbcca”
Output: 1

Approach: The given problem can be solved by using Two Pointers by finding similar characters present in the suffix of the string and the prefix of the string
Follow the steps below to solve the problem:

• Initialize two variables, say i as 0 and j as (N – 1).
• Iterate a loop until i < j and the characters S[i] and S[j] are the same, and perform the following steps:
• Initialize a variable, say d with S[i], and shift pointer i towards the right while i is at most j and d = S[i].
• Shift pointer j towards the left until i is at most j and d = S[j].
• After completing the above steps, the value of (j – i + 1) is the minimum possible length of the string.

Below is the implementation of the above approach:

## C++

// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;

// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
int minLength(string s)
{

// Initialize two pointers
int i = 0, j = s.length() - 1;

// Traverse the string S
for(; i < j && s[i] == s[j];)
{

// Current char on left pointer
char d = s[i];

// Shift i towards right
while (i <= j && s[i] == d)
i++;

// Shift j towards left
while (i <= j && s[j] == d)
j--;
}

// Return the minimum possible
// length of string
return j - i + 1;
}

// Driver Code
int main()
{
string S = "aacbcca";

cout << minLength(S);
}

// This code is contributed by bgangwar59

## Java

// Java program for the above approach

import java.lang.*;
import java.util.*;

class GFG {

// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
static int minLength(String s)
{
// Initialize two pointers
int i = 0, j = s.length() - 1;

// Traverse the string S
for (; i < j
&& s.charAt(i) == s.charAt(j);) {

// Current char on left pointer
char d = s.charAt(i);

// Shift i towards right
while (i <= j
&& s.charAt(i) == d)
i++;

// Shift j towards left
while (i <= j
&& s.charAt(j) == d)
j--;
}

// Return the minimum possible
// length of string
return j - i + 1;
}

// Driver Code
public static void main(String[] args)
{
String S = "aacbcca";
System.out.println(minLength(S));
}
}

## Python3

# Python3 program for the above approach

# Function to find the minimum length
# of the string after removing the same
# characters from the end and front of the
# two strings after dividing into 2 substrings
def minLength(s):

# Initialize two pointers
i = 0; j = len(s) - 1

# Traverse the string S
while (i < j and s[i] == s[j]):

# Current char on left pointer
d = s[i]

# Shift i towards right
while (i <= j and s[i] == d):
i += 1

# Shift j towards left
while (i <= j and s[j] == d):
j -= 1

# Return the minimum possible
# length of string
return j - i + 1

# Driver Code
if __name__ == "__main__" :

S = "aacbcca"

print(minLength(S))

# This code is contributed by AnkThon

## C#

// C# program for the above approach
using System;

class GFG{

// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
static int minLength(string s)
{

// Initialize two pointers
int i = 0, j = s.Length - 1;

// Traverse the string S
for(; i < j && s[i] == s[j];)
{

// Current char on left pointer
char d = s[i];

// Shift i towards right
while (i <= j && s[i] == d)
i++;

// Shift j towards left
while (i <= j && s[j] == d)
j--;
}

// Return the minimum possible
// length of string
return j - i + 1;
}

// Driver Code
public static void Main(string[] args)
{
string S = "aacbcca";

Console.WriteLine(minLength(S));
}
}

// This code is contributed by AnkThon

## Javascript

<script>
// JavaScript program for the above approach

// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
function minLength(s)
{

// Initialize two pointers
i = 0; j = s.length - 1

// Traverse the string S
while (i < j && s[i] == s[j]){

// Current char on left pointer
d = s[i]

// Shift i towards right
while (i <= j && s[i] == d)
i += 1

// Shift j towards left
while (i <= j && s[j] == d)
j -= 1

// Return the minimum possible
// length of string
}
return j - i + 1
}

// Driver Code
S = "aacbcca"

document.write(minLength(S))

// This code is contributed by AnkThon
</script>
Output:

1

Time Complexity: O(N)
Auxiliary Space: O(1) because no extra space is used except for variables

My Personal Notes arrow_drop_up