One In Another
In this program you are given with two strings, you need to find character of string2 in string1, once it is find then get index of that character and return the same else returns zero.
Input:
adf6ysh
123456
output: 3
Input:
adf6ysh
123455
output: 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class HackerRank { | |
public static int oneInAnother(String input1,String input2) | |
{ | |
int index=0; | |
String [] input1Array=new String [input1.length()]; | |
String [] input2Array=new String [input2.length()]; | |
input1Array=input1.split(""); //converting string to string Array | |
input2Array=input2.split(""); //converting string to string Array | |
for(int i=0;i<input2Array.length;i++){ | |
String a=String.valueOf(input2Array[i]); | |
for(int j=0;j<input1Array.length;j++){ | |
String b=String.valueOf(input1Array[j]); | |
if(a.equals(b)){ | |
index=j; | |
break; | |
} | |
} | |
} | |
return index; | |
} | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int output = 0; | |
String ip1 = in.nextLine().trim(); | |
String ip2 = in.nextLine().trim(); | |
output = oneInAnother(ip1,ip2); | |
System.out.println(String.valueOf(output)); | |
in.close(); | |
} | |
} |
Input:
adf6ysh
123456
output: 3
Input:
adf6ysh
123455
output: 0
Comments
Post a Comment