import java.lang.*;
public class similar
{
static void find(String s1,String s2)
{
StringBuffer buf1 = new StringBuffer(s1);
StringBuffer buf2 = new StringBuffer(s2);
int count=0;
int l=0,q=0;
int[] arr=new int[30];
int[] arr1=new int[30];//required minimum size 26
for(char j='A';j<='Z';j++)
{
for(int i=0;i<buf1.length();i++)
{
if(buf1.charAt(i)==j) //compare the string1 indexes with Alphabets
{
count++; //count similar alphabets
}
}
arr[l]=count;
count=0;
l++;
}
for(char j='A';j<='Z';j++)
{
for(int i=0;i<buf2.length();i++)
{
if(buf2.charAt(i)==j)//compare the string2 indexes with Alphabets
{
count++; //count similar alphabets
}
}
arr1[q]=count;
count=0;
q++;
}
StringBuffer answer=new StringBuffer();
count=0;
int f=0;
for(char ch='A';ch<='Z';ch++)
{
if(arr[f]!=0&&arr1[f]!=0)
{
count++;
answer.append(ch);
}
f++;
}
//print the common string
System.out.println(answer);//Contain the string with common alphabets
}
public static void main(String[] args)
{
String s1=new String("AMITABH BACHCHAN");
String s2=new String("RAJNIKANTH");
find(s1,s2);
}
}
output
AHINT
Không có nhận xét nào:
Đăng nhận xét