Minimize length of a string by removing suffixes and prefixes of same characters
Last Updated :
10 Oct, 2022
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++
#include<bits/stdc++.h>
using namespace std;
int minLength(string s)
{
int i = 0, j = s.length() - 1;
for (; i < j && s[i] == s[j];)
{
char d = s[i];
while (i <= j && s[i] == d)
i++;
while (i <= j && s[j] == d)
j--;
}
return j - i + 1;
}
int main()
{
string S = "aacbcca" ;
cout << minLength(S);
}
|
Java
import java.lang.*;
import java.util.*;
class GFG {
static int minLength(String s)
{
int i = 0 , j = s.length() - 1 ;
for (; i < j
&& s.charAt(i) == s.charAt(j);) {
char d = s.charAt(i);
while (i <= j
&& s.charAt(i) == d)
i++;
while (i <= j
&& s.charAt(j) == d)
j--;
}
return j - i + 1 ;
}
public static void main(String[] args)
{
String S = "aacbcca" ;
System.out.println(minLength(S));
}
}
|
Python3
def minLength(s):
i = 0 ; j = len (s) - 1
while (i < j and s[i] = = s[j]):
d = s[i]
while (i < = j and s[i] = = d):
i + = 1
while (i < = j and s[j] = = d):
j - = 1
return j - i + 1
if __name__ = = "__main__" :
S = "aacbcca"
print (minLength(S))
|
C#
using System;
class GFG{
static int minLength( string s)
{
int i = 0, j = s.Length - 1;
for (; i < j && s[i] == s[j];)
{
char d = s[i];
while (i <= j && s[i] == d)
i++;
while (i <= j && s[j] == d)
j--;
}
return j - i + 1;
}
public static void Main( string [] args)
{
string S = "aacbcca" ;
Console.WriteLine(minLength(S));
}
}
|
Javascript
<script>
function minLength(s)
{
i = 0; j = s.length - 1
while (i < j && s[i] == s[j]){
d = s[i]
while (i <= j && s[i] == d)
i += 1
while (i <= j && s[j] == d)
j -= 1
}
return j - i + 1
}
S = "aacbcca"
document.write(minLength(S))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) because no extra space is used except for variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...