what is wrong with my code for "your name is mine"

this is my code for “your name is mine” please tell me why my answer is rejected though my output is right?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//your name is my name
class name {

public String str_a,str_b;// string var to store the two strings
public static void main(String[] args) {
	// TODO Auto-generated method stub
	try{
		name obj=new name();
	
	System.out.println("enter the two strings\n");
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	obj.str_a=br.readLine();
	obj.str_b=br.readLine();
	obj.func_x(obj.str_a);
	}catch(IOException e){
		e.printStackTrace();
	}
}


void func_x(String str_a){// this function maintains an index counter,dat contains the index of the matched charecter
	
	int index=0;
	int i=0;
	while(i<str_a.length()){
		index=func_y(index,str_a.charAt(i));
		if(index!=-1){
			index++;
			i++;
		}
		else 
			break;
	}
	
	if(i!=str_a.length())
		System.out.println("NO");
	else
		System.out.println("YES");
	
}


int func_y(int start_index,char k){
	
	for(int i=start_index;i<str_b.length();i++){// as the charecters have 2 appear in order,if the matching charecter
		// is found its index is returned and search for the next valid charecter is started from the index value prev
		// returned
		if(k==str_b.charAt(i))
			return(i);
			
	}
	return(-1);
		
	
}

}

You dont have to print anything other than ans.
So remove this line

System.out.println("enter the two strings\n");

thank you for your rply…did that now it says run time erroe(NZEC), though the code works fine on my system…please help

You have to take input in one line.

if you are using Bufferedreader then you have to use split function, or use Scanner

1 Like