Given string str of size N. The task is to write a recursive function to replace all occurrences of pi with 3.14 in the given string and print the modified string.
Examples:
Input : str = “pippppiiiipi”
Output : 3.14ppp3.14iii3.14
Input : str = “pip”
Output : 3.14p
Input : str = “xpix”
Output : x3.14x
We have discussed an iterative function here
Approach :
- If there is only one character in a string or the string is empty break the recursive call
- Else keep the first character of the string with yourself and pass the rest of the string to recursion.
- If the first character is not ‘p’ then just put that character in front of the answer which came from recursion
- Else if the first character is ‘p’ and the first character of the part passed to recursion is ‘i’ then replace “pi” with “3.14”
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void replacePiHelper( char str[], int start)
{
if (str[start] == '\0' || str[start + 1] == '\0' ) {
return ;
}
replacePiHelper(str, start + 1);
if (str[start] == 'p' && str[start + 1] == 'i' ) {
for ( int i = strlen (str); i >= start + 2; i--) {
str[i + 2] = str[i];
}
str[start] = '3' ;
str[start + 1] = '.' ;
str[start + 2] = '1' ;
str[start + 3] = '4' ;
}
}
void replacePi( char str[])
{
replacePiHelper(str, 0);
}
int main()
{
char str[] = "pippppiiiipi" ;
replacePi(str);
cout << str;
return 0;
}
|
Java
class GFG {
public String replacePi(String str)
{
if (str.length() <= 1 ) {
return str;
}
if (str.charAt( 0 ) == 'p' && str.length() >= 2
&& str.charAt( 1 ) == 'i' ) {
return "3.14" + replacePi(str.substring( 2 , str.length()));
}
return str.charAt( 0 ) + replacePi(str.substring( 1 , str.length()));
}
public static void main(String args[])
{
GFG g = new GFG();
String str = "pippppiiiipi" ;
System.out.println(g.replacePi(str));
}
}
|
Python3
def replacePieHelper(string, start):
if len (string) < 2 or start = = len (string):
return string
replacePieHelper(string, start + 1 )
if (string[start] = = 'p' and
string[start + 1 ] = = 'i' ):
string[start:start + 2 ] = [ '3' , '.' , '1' , '4' ]
def replacePi(string):
replacePieHelper(string, 0 )
if __name__ = = "__main__" :
string = "pippppiiiipi"
string = list (string)
replacePi(string)
string = ''.join(string)
print (string)
|
C#
using System;
class gfg {
public String replacePi(String str)
{
if (str.Length <= 1) {
return str;
}
if (str[0] == 'p' && str.Length >= 2
&& str[1] == 'i' ) {
return "3.14" + replacePi(str.Substring(2, str.Length - 2));
}
return str[0] + replacePi(str.Substring(1, str.Length - 1));
}
}
class geek {
public static int Main()
{
gfg g = new gfg();
string input = "pippppiiiipi" ;
Console.WriteLine(g.replacePi(input));
return 0;
}
}
|
Javascript
<script>
function replacePi(str) {
if (str.length <= 1) {
return str;
}
if (str[0] === "p" && str.length >= 2 && str[1] === "i" ) {
return "3.14" + replacePi(str.substring(2, str.length));
}
return str[0] + replacePi(str.substring(1, str.length));
}
var input = "pippppiiiipi" ;
document.write(replacePi(input));
</script>
|
Output
3.14ppp3.14iii3.14
Time complexity: O(N) where N is length of given string
Auxiliary space: O(N) for call stack
Another Approach:
A simple recursive approach to replace all pi in a given function with “3.14”. Firstly function is declared we don’t need any helper function.
- Base case if the string is empty or the length of the string is 1 return the string.
- If the 0th and 1st element of the string is p and we have to handle them for the rest we have to call recursion it will give the result.
- If not then we have to call recursion from 1st to all elements then add recursion result to 1st element and return it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string replacePi(string s)
{
if (s.length() == 0 || s.length() == 1)
return s;
if (s[0] == 'p' && s[1] == 'i' ) {
string smallOutput = replacePi(s.substr(2));
return "3.14" + smallOutput;
}
else {
return s[0] + replacePi(s.substr(1));
}
}
int main()
{
string s = "pipppiiipi" ;
string result = replacePi(s);
cout << result << endl;
return 0;
}
|
Java
class GFG {
public static String replacePi(String s)
{
if (s.length() == 0 || s.length() == 1 )
return s;
if (s.charAt( 0 ) == 'p' && s.charAt( 1 ) == 'i' ) {
String smallOutput = replacePi(s.substring( 2 ));
return "3.14" + smallOutput;
}
else {
String smallOutput = replacePi(s.substring( 1 ));
return s.charAt( 0 ) + smallOutput;
}
}
public static void main(String[] args)
{
String s = "pipppiiipi" ;
String result = replacePi(s);
System.out.println(result);
}
}
|
Python
def replacePi(string):
if len (string) = = 0 or
len (string) = = 1 :
return string
if string[ 0 ] = = 'p' and string[ 1 ] = = 'i' :
smallOutput = replacePi(string[ 2 :])
return "3.14" + smallOutput
else :
smallOutput = replacePi(string[ 1 :])
return string[ 0 ] + smallOutput
if __name__ = = "__main__" :
string = "pipppiiipi"
result = replacePi(string)
print result
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class GFG {
static string replacePi( string s)
{
if (s.Length == 0 || s.Length == 1)
return s;
if (s[0] == 'p' && s[1] == 'i' ) {
string smallOutput = replacePi(s.Substring(2));
return "3.14" + smallOutput;
}
else {
string smallOutput = replacePi(s.Substring(1));
return s[0] + smallOutput;
}
}
public static void Main( string [] args)
{
string s = "pipppiiipi" ;
string result = replacePi(s);
Console.Write(result);
}
}
|
Javascript
<script>
function replacePi(s)
{
if (s.length == 0 || s.length == 1)
return s;
if (s[0] == 'p ' && s[1] == ' i ') {
// Smalloutput is a variable
// used to store recursion result
let smallOutput = replacePi(s.substr(2));
// And we have to add the recursion
// result with the first part we
// handled and return the answer
return "3.14" + smallOutput;
}
else {
// If 1st & 2nd element aren' t "p" & "i" , then keep
return s[0] + replacePi(s.substr(1));
}
}
let s = "pipppiiipi" ;
let result = replacePi(s);
document.write(result);
</script>
|
Time complexity: O(N), where N is the length of the given string
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Nov, 2022
Like Article
Save Article