Minimize length of a string by removing occurrences of another string from it as a substring
Given a string S and a string T, the task is to find the minimum possible length to which the string S can be reduced to after removing all possible occurrences of string T as a substring in string S.
Examples:
Input: S = “aabcbcbd”, T = “abc”
Output: 2
Explanation:
Removing the substring {S[1], …, S[3]} and modifies the remaining string to “abcbd”.
Removing the substring {S[0] .. S[2]}, the resultant string modifies to “bd”.
Therefore, the required answer is 2.
Input: S = “asdfbc”, T = “xyz”
Output: 0
Explanation:
No occurrence of the string “xyz” as a substring in S.
Approach: The idea is to iterate over the characters of the given string and initialize an auxiliary string and check if the newly formed string is present as a substring in the given string. If found to be true, then simply remove that substring from the given string.
Follow the steps below to solve this problem:
- First, identify the substrings T by traversing through the string and keeping track of the characters encountered.
- But, when the substring is removed, the concatenation of the remaining parts is expensive as each character has to move backwards by M places.
- In order to avoid this, maintain a string, say temp, which contains only the characters iterated so far.
- Hence, if the required substring is present in temp, then just remove the last M characters in constant computational time.
- Finally, print the minimized length of the string after performing all operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minLength(string& S, string& T,
int N, int M)
{
string temp;
int subtract = 0;
for ( int i = 0; i < N; ++i) {
temp.push_back(S[i]);
if (temp.size() >= M) {
if (temp.substr(temp.size() - M, M) == T) {
subtract += M;
int cnt = 0;
while (cnt != M) {
temp.pop_back();
++cnt;
}
}
}
}
cout << (N - subtract) << "\n" ;
}
int main()
{
string S = "aabcbcbd" , T = "abc" ;
int N = S.size();
int M = T.size();
minLength(S, T, N, M);
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void minLength(String S, String T,
int N, int M)
{
String temp = "" ;
int subtract = 0 ;
for ( int i = 0 ; i < N; ++i)
{
temp += S.charAt(i);
if (temp.length() >= M)
{
if (T.equals(
temp.substring(temp.length() - M,
temp.length())))
{
subtract += M;
int cnt = 0 ;
while (cnt != M)
{
temp = temp.substring(
0 , temp.length() - 1 );
++cnt;
}
}
}
}
System.out.println((N - subtract));
}
public static void main(String[] args)
{
String S = "aabcbcbd" , T = "abc" ;
int N = S.length();
int M = T.length();
minLength(S, T, N, M);
}
}
|
Python3
def minLength(S, T, N, M):
temp = "";
subtract = 0 ;
for i in range (N):
temp + = S[i];
if ( len (temp) > = M):
if (T = = (temp[ len (temp) - M: len (temp)])):
subtract + = M;
cnt = 0 ;
while (cnt ! = M):
temp = temp[ 0 : len (temp) - 1 ];
cnt + = 1 ;
print ((N - subtract));
if __name__ = = '__main__' :
S = "aabcbcbd" ;
T = "abc" ;
N = len (S);
M = len (T);
minLength(S, T, N, M);
|
C#
using System;
class GFG{
static void minLength(String S, String T,
int N, int M)
{
String temp = "" ;
int subtract = 0;
for ( int i = 0; i < N; ++i)
{
temp += S[i];
if (temp.Length >= M)
{
if (T.Equals(
temp.Substring(temp.Length - M, M)))
{
subtract += M;
int cnt = 0;
while (cnt != M)
{
temp = temp.Substring(
0, temp.Length - 1);
++cnt;
}
}
}
}
Console.WriteLine((N - subtract));
}
public static void Main(String[] args)
{
String S = "aabcbcbd" , T = "abc" ;
int N = S.Length;
int M = T.Length;
minLength(S, T, N, M);
}
}
|
Javascript
<script>
function minLength(S, T,
N, M)
{
let temp = "" ;
let subtract = 0;
for (let i = 0; i < N; ++i)
{
temp += S[i];
if (temp.length >= M)
{
if (T ==
temp.substr(temp.length - M,
temp.length))
{
subtract += M;
let cnt = 0;
while (cnt != M)
{
temp = temp.substr(
0, temp.length - 1);
++cnt;
}
}
}
}
document.write((N - subtract));
}
let S = "aabcbcbd" , T = "abc" ;
let N = S.length;
let M = T.length;
minLength(S, T, N, M);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
09 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...