Replace by X

 Given a string and a pattern, Replace all the continuous occurrence of pattern with a single X in the string. 



Example:

Input
2
abababcdefababcdab
ab
codemummycode
code

Output
XcdefXcdX
XmummyX



 

c++ implementation:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	
	while(t--)
	{
	    string s;string s1;
	    cin>>s>>s1;
	    int i=0;
	    string z="";
	    while(i<s.size())
	    {
	        string temp=s.substr(i,s1.size());
	        
	        if(temp==s1)
	        {
	            if(z[z.size()-1]!='X')
	            z+='X';
	            
	            i+=s1.size();
	            
	        }
	        else
	        {
	           z+=s[i];
	            i++;
	        }
	    }
	    
	    cout<<z<<endl;
	}
	
	return 0;
}


Time Complexity: O(n) 
space Complexity: 0(n)

n is the length of first string or in the above program s string.








No comments

darkmode