Answer is correct

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc= new Scanner (System.in);
int test= sc.nextInt();
for(int i=0;i<test;i++){
String s= sc.nextLine();
System.out.println(pi(s));
}

}

public static String pi(String s){
	if(s.length()==1||s.length()==0){
		return s;
	}
	if(s.charAt(0)=='p'&&s.charAt(1)=='i'){
		s="3.14"+pi(s.substring(2,s.length()));
	}else {
		s=s.charAt(0)+pi(s.substring(1,s.length()));
	}
	return s;
}

}

Hi @Hardik-Bhardwaj-392610454600906
For input
3
xpix
xabpixx3.15x
xpipippixx
Your code is giving output :-

x3.14x
xab3.14xx3.15x
But Actual output that should come is :-
x3.14x
xab3.14xx3.15x
x3.143.14p3.14xx
Use this approach in your you will be able to solve this problem

  • Traverse the string. At each instance check whether substring starting from index 0 till index 2 is same as β€œpi”.
  • If so , obtain the result for remaining string from index 2 i.e. s.substring(2) recursively . Store this result in say β€˜ros’ . Now return β€œ3.14” + ros.
  • If however , it was not so , obtain the result for the remaining string starting from index 1 and add s[0] to it i.e. return s[0] + ros.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hi @Hardik-Bhardwaj-392610454600906