logo

Python – Nahradí všetky výskyty podreťazca v reťazci

Niekedy pri práci s reťazcami Pythonu môžeme mať problém, keď potrebujeme nahradiť všetky výskyty podreťazca inými.

Vstup: test_str = geeksforgeeks s1 = geeks s2 = abcd
Výkon : test_str = abcdforabcd Vysvetlenie: Všetky výskyty s1 nahradíme s2 v test_str.

Vstup: test_str = geeksforgeeks s1 = pre s2 = abcd
Výkon : test_str = geeksabcdgeeks



Prístup 1

Môžeme použiť vstavanú funkciu nahradiť prítomnú v python3 na nahradenie všetkých výskytov podreťazca.

Implementácia pomocou vstavanej funkcie: -

Python3




#Python has inbuilt function replace to replace all occurrences of substring.> input_string>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> input_string>=> input_string.replace(s1, s2)> print>(input_string)>

formátovať dátum v jave
>

>

Výkon

abcdforabcd>

Časová zložitosť: O(n)
Pomocný priestor: O(n)

Prístup 2:

Používa sa rozdelenie reťazca podreťazcom a následné nahradenie novou funkciou string.split().

Python3

vicky kaushal vek




#code for replacing all occurrences of substring s1 with new string s2> test_str>=>'geeksforgeeks'> s1>=>'geeks'> s2>=>'abcd'> #string split by substring> s>=>test_str.split(s1)> new_str>=>''> for> i>in> s:> >if>(i>=>=>''):> >new_str>+>=>s2> >else>:> >new_str>+>=>i> #printing the replaced string> print>(new_str)> #contributed by Bhavya Koganti>

>

>

Výkon

abcdforabcd>

Časová a priestorová zložitosť pre všetky metódy je rovnaká:

Časová zložitosť: O(n)

Pomocný priestor: O(n)

Metóda 3: Ďalším prístupom na nahradenie všetkých výskytov podreťazca v reťazci je použitie re.sub() funkciu z modulu re v pythone.

Python3




import> re> def> replace_substring(test_str, s1, s2):> ># Replacing all occurrences of substring s1 with s2> >test_str>=> re.sub(s1, s2, test_str)> >return> test_str> # test> test_str>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> print>(replace_substring(test_str, s1, s2))>

>

>

Výkon

abcdforabcd>

Časová zložitosť: O(n), kde n je dĺžka vstupného reťazca. Je to preto, že funkcia re.sub() iteruje cez celý vstupný reťazec a vykoná zhodu regulárneho výrazu s každým znakom, aby našla všetky výskyty podreťazca. Počet iterácií je priamo úmerný dĺžke vstupného reťazca.
Pomocný priestor: Nový

Metóda 4: Použitie jednoduchej iterácie

Myšlienkou tohto prístupu je opakovanie vstupného reťazca znak po znaku a kontrola, či sa každý podreťazec dĺžky m zhoduje s podreťazcom, ktorý chceme nahradiť. Ak áno, pridáme náhradný podreťazec do nášho výsledku a posunieme ukazovateľ dopredu o m znakov. Ak sa nezhoduje, pridáme k výsledku aktuálny znak a posunieme ukazovateľ dopredu o 1 znak.

Python3




def> replace_substring(test_str, s1, s2):> ># Initialize an empty string to store the result> >result>=> ''> ># Initialize a variable to keep track of our position in the string> >i>=> 0> ># Loop through the string one character at a time> >while> i <>len>(test_str):> ># Check if the current substring matches the substring we want to replace> >if> test_str[i:i>+>len>(s1)]>=>=> s1:> ># If it does, add the replacement substring to the result and move the pointer forward> >result>+>=> s2> >i>+>=> len>(s1)> >else>:> ># If it doesn't, add the current character to the result and move the pointer forward> >result>+>=> test_str[i]> >i>+>=> 1> ># Return the final result> >return> result> # test> test_str>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> print>(replace_substring(test_str, s1, s2))>

>

linuxový správca úloh
>

Výkon

abcdforabcd>

Časová zložitosť: O(nm), kde n je dĺžka vstupného reťazca a m je dĺžka podreťazca, ktorý sa má nahradiť.
Pomocný priestor: O(n), pretože vytvárame nový reťazec na uloženie výsledku.