AlgoUniversity
  • Go Back
Discussion
String Substitution Hard :

Author

Gigaliths

Difficulty Level : Hard

Submissions : 327

Asked In : Google

Marks :10

: 6 | : 3

You are given a string $$$S$$$ , and a list of strings which needs to be substituted in the input string $$$S$$$.

A substitution is a tuple $$$( L_i , R_i , subs_i ) $$$ which denotes that we need to substitute the substring $$$S[ L_i ... R_i ]$$$ with $$$subs_i$$$.

The intervals in this problem can be overlapping. Incase multiple substitutions overlap with each other , substitute the first substitution over minimum_L and maximum_R obtained across all overlapping substitutions.

You must find out the modified string $$$S_m$$$ after incurring all substitutions.

Example

Let S = "aaaaaaaaa"

Let subs = [ (4,6,"hi") , (1,2,"bye") , (2,5,"ohho") , (7,7,"hehe")];

When we reach at index i = 1 , we see that the range 1,2 overlaps 2,5 which overlaps 4,6 , Hence 1,2 indirectly overlaps 4,6 (which comes first based on index). The combined substitution becomes [ (1 , 6 , "hi") ] , The substitution string is always the string that has the comes from the least index across all overlapping substitution strings.

$$$S_m$$$ = "ahihehea"

Input

First line contains a string corresponding to $$$S$$$

Second line contains a single integer N corresponding to the total number of substitutions

N lines are followed containing each tuple $$$( L_i , R_i , subs_i )$$$

Contraints

$$$ 1 \le |S| \le 100000 $$$

$$$ 1 \le |N| \le 1000 $$$

$$$ 1 \le L_i \le R_i \le |S| $$$

All characters in the string are either smaller case alphabetic characters or '_'

Output

A single string $$$ S_m $$$ which corresponds to the modified string after all substitutions.

Example

Input
a_b_c_d_e_f_g
4
4 6 hi
1 2 bye
2 5 ohho
7 7 hehe
Output
ahihehee_f_g

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.