Find and Replace in String | GeeksforGeeks Problem of the Day

preview_player
Показать описание
Use coupon ALISHA on any GeeksforGeeks course to get 10% discount:

Join this channel to get access to perks:

Check out our other playlists:

Dynamic Programming:
Trees:
Heaps and Maps:
Arrays and Maths:
Bit Manipulation:
Greedy Algorithms:
Sorting and Searching:
Strings:

Linked Lists:
Stack and Queues:
Two Pointers:
Graphs, BFS, DFS:
Backtracking:

Non- DSA playlists:
Probability:
SQL-Basic Join functions:
SQL-Basic Aggregate functions:
Рекомендации по теме
Комментарии
Автор

Code in CPP:
string original = S;
int newlength = 0;
for(int i=0;i<Q;i++)
{
auto found = original.find(sources[i]);

while(found!=string::npos)
{
if(found == index[i])
{
S.replace(found+newlength, sources[i].size(), targets[i]);


}
found = original.find(sources[i], found+1);
}


}




return S;

probabilitycodingisfunis
Автор

Easiest solution java with o(n)
class Solution {
static String findAndReplace(String S, int Q, int[] index,
String[] sources, String[] targets)
{
String p="";
String temp=S;
int ct=0;
for(int i=0;i<Q;i++)
{
int x=S.indexOf(sources[i], index[i]-temp.length()+S.length());
if(index[i]+ct==x)
{
String bef=S.substring(0, x);
String mid=S.substring(x, x+sources[i].length());
String
S=bef+mid.replace(sources[i], targets[i])+aft;
ct=S.length()-temp.length();
//
}
}
return S;
}
};

manannagpal
Автор

Offsets are okay but it is way better to sort by indexes from the right side and then eventually do the operations in that way the new indexes stay unaffected

MGtvMusic
Автор

Aap bhot acha kam karti ho devi...May Lord krishna bless you 😇

ronaktiwari
Автор

This code is giving error on Leetcode: 833(just because of NPOS)

hindiexpress
Автор

Time complexity of this will be n^2 but?

shanktech
Автор

Great Video, If I needed assistance on data structures assignment for class would you be able to assist me?

arjunragu