Given two string S1 and S2. The task is to wrap every instance of string S2 in string S1 with some string on either side.
Note: Here we will use underscore(_) for the wrapping part. It can be anything as per need, for example some HTML tag, white space, etc.
Input: S1 = “examwill be a examexam”, S2 = “exam”
Output: “_exam_will be a _examexam_”
String S2 is “exam“, so wrap occurrence of S2 with underscore. Since, last two occurance overlaps(in string examexam), so merge both the occurrence and wrap by putting underscore to farther left and farther right of merged substring.
Input: str = “abcabcabcabc”, substr = “abc”
Approach: The idea is to simply get the location of all instances of string S2 in S1 and add underscore at either end of the occurrence and if two or more instances of S2 overlaps then, add underscore at either end of the merged substring. Below are the steps:
- Get the location of all instances of the string S2 in the main string S1. For that traverse string S1 one character at a time and call substring-matching function, find().
- Create a 2D array of locations(say arr), where each subarray holds the starting and ending indices of a specific instance of the string S2 in the string S1.
- Merge the overlapping starting and ending index intervals stored in arr.
- After the above steps all the overlapping indexes are merged. Now traverse the given string and intervals at the same time and create a new string by wrapping underscores.
- Print the new string after the above step.
Below is the implementation of the above approach:
_exam_will be a _examexam_
Time Complexity: O(N * M), where N and M is the length of the string S1 and S2 respectively.
Auxiliary Space: O(N), where N is the length of string S1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Word Wrap problem ( Space optimized solution )
- Find largest word in dictionary by deleting some characters of given string
- String containing first letter of every word in a given string with spaces
- Find the word from a given sentence having given word as prefix
- C program to Replace a word in a text by another given word
- C program to find and replace a word in a File by another given word
- Longest Common Prefix using Word by Word Matching
- Reverse every word of the string except the first and the last character
- Min flips of continuous characters to make all characters same in a string
- Modify the string by swapping continuous vowels or consonants
- Find shortest unique prefix for every word in a given list | Set 2 (Using Sorting)
- Minimum letters to be removed to make all occurrences of a given letter continuous
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Most frequent word in first String which is not present in second String
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Maximize the value of A by replacing some of its digits with digits of B
- Count distinct substrings that contain some characters at most k times
- Divide N into K parts in the form (X, 2X, ... , KX) for some value of X
- Calculate the frequency of each word in the given string
- Minimum string such that every adjacent character of given string is still adjacent
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet