Minimize a string by removing all occurrences of another string
Given two strings S1 and S2 of length N and M respectively, consisting of lowercase letters, the task is to find the minimum length to which S1 can be reduced by removing all occurrences of the string S2 from the string S1.
Examples:
Input: S1 =”fffoxoxoxfxo”, S2 = “fox”;
Output: 3
Explanation:
By removing “fox” starting from index 2, the string modifies to “ffoxoxfxo”.
By removing “fox” starting from index 1, the string modifies to “foxfxo”.
By removing “fox” starting from index 0, the string modifies to “fxo”.
Therefore, the minimum length of string S1 after removing all occurrences of S2 is 3.
Input: S1 =”abcd”, S2 = “pqr”
Output: 4
Approach: The idea to solve this problem is to use Stack Data Structure. Follow the steps below to solve the given problem:
- Initialize a stack to store the characters of the string S1 in it.
- Traverse the given string S1 and perform the following operations:
- After completing the above steps, the remaining size of the stack is the required minimum length.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minLength(string str, int N,
string K, int M)
{
stack< char > stackOfChar;
for ( int i = 0; i < N; i++) {
stackOfChar.push(str[i]);
if (stackOfChar.size() >= M) {
string l = "" ;
for ( int j = M - 1; j >= 0; j--) {
if (K[j] != stackOfChar.top()) {
int f = 0;
while (f != l.size()) {
stackOfChar.push(l[f]);
f++;
}
break ;
}
else {
l = stackOfChar.top()
+ l;
stackOfChar.pop();
}
}
}
}
return stackOfChar.size();
}
int main()
{
string S1 = "fffoxoxoxfxo" ;
string S2 = "fox" ;
int N = S1.length();
int M = S2.length();
cout << minLength(S1, N, S2, M);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minLength(String str, int N,
String K, int M)
{
Stack<Character> stackOfChar = new Stack<Character>();
for ( int i = 0 ; i < N; i++)
{
stackOfChar.add(str.charAt(i));
if (stackOfChar.size() >= M)
{
String l = "" ;
for ( int j = M - 1 ; j >= 0 ; j--)
{
if (K.charAt(j) != stackOfChar.peek())
{
int f = 0 ;
while (f != l.length())
{
stackOfChar.add(l.charAt(f));
f++;
}
break ;
}
else
{
l = stackOfChar.peek()
+ l;
stackOfChar.pop();
}
}
}
}
return stackOfChar.size();
}
public static void main(String[] args)
{
String S1 = "fffoxoxoxfxo" ;
String S2 = "fox" ;
int N = S1.length();
int M = S2.length();
System.out.print(minLength(S1, N, S2, M));
}
}
|
Python3
def minLength( Str , N, K, M) :
stackOfChar = []
for i in range (N) :
stackOfChar.append( Str [i])
if ( len (stackOfChar) > = M) :
l = ""
for j in range (M - 1 , - 1 , - 1 ) :
if (K[j] ! = stackOfChar[ - 1 ]) :
f = 0
while (f ! = len (l)) :
stackOfChar.append(l[f])
f + = 1
break
else :
l = stackOfChar[ - 1 ] + l
stackOfChar.pop()
return len (stackOfChar)
S1 = "fffoxoxoxfxo"
S2 = "fox"
N = len (S1)
M = len (S2)
print (minLength(S1, N, S2, M))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minLength(String str, int N,
String K, int M)
{
Stack< char > stackOfChar = new Stack< char >();
for ( int i = 0; i < N; i++)
{
stackOfChar.Push(str[i]);
if (stackOfChar.Count >= M)
{
String l = "" ;
for ( int j = M - 1; j >= 0; j--)
{
if (K[j] != stackOfChar.Peek())
{
int f = 0;
while (f != l.Length)
{
stackOfChar.Push(l[f]);
f++;
}
break ;
}
else
{
l = stackOfChar.Peek()
+ l;
stackOfChar.Pop();
}
}
}
}
return stackOfChar.Count;
}
public static void Main(String[] args)
{
String S1 = "fffoxoxoxfxo" ;
String S2 = "fox" ;
int N = S1.Length;
int M = S2.Length;
Console.Write(minLength(S1, N, S2, M));
}
}
|
Javascript
<script>
function minLength(str, N, K, M)
{
var stackOfChar = [];
for ( var i = 0; i < N; i++) {
stackOfChar.push(str[i]);
if (stackOfChar.length >= M) {
var l = "" ;
for ( var j = M - 1; j >= 0; j--) {
if (K[j] != stackOfChar[stackOfChar.length-1]) {
var f = 0;
while (f != l.length) {
stackOfChar.push(l[f]);
f++;
}
break ;
}
else {
l = stackOfChar[stackOfChar.length-1]
+ l;
stackOfChar.pop();
}
}
}
}
return stackOfChar.length;
}
var S1 = "fffoxoxoxfxo" ;
var S2 = "fox" ;
var N = S1.length;
var M = S2.length;
document.write( minLength(S1, N, S2, M));
</script>
|
Time complexity: O(N*M), as we are using nested loops for traversing N*M times.
Auxiliary Space: O(N), as we are using extra space for stack.
Last Updated :
27 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...