problem no 56:(problem on Strings):Given two strings A and B. Find the characters that are not common in the two strings.
QUESTION:
Given two strings A and B. Find the characters that are not common in the two strings.
Example 1:
Input:
A = geeksforgeeks
B = geeksquiz
Output: fioqruz
Explanation:
The characters 'f', 'i', 'o', 'q', 'r', 'u','z'
are either present in A or B, but not in both.
Example 2:
Input:
A = characters
B = alphabets
Output: bclpr
Explanation: The characters 'b','c','l','p','r'
are either present in A or B, but not in both.
Your Task:
You dont need to read input or print anything. Complete the function UncommonChars()
which takes strings A and B as input parameters and returns a string
that contains all the uncommon characters in sorted order. If no such
character exists return "-1".
ANSWER:
String UncommonChars(String A, String B)
{
int arr1[]=new int[128];
int arr2[]=new int[128];
for(int i=0;i<128;i++)
{
arr1[i]=0;
arr2[i]=0;
}
for(int i=0;i<A.length();i++)
{
arr1[(int)A.charAt(i)]=arr1[(int)A.charAt(i)]+1;
}
for(int i=0;i<B.length();i++)
{
arr2[(int)B.charAt(i)]=arr2[(int)B.charAt(i)]+1;
}
String s="";
for(int i=0;i<128;i++)
{
if(arr1[i]>0 && arr2[i]==0)
{
s=s+(char)i;
}
if(arr2[i]>0 && arr1[i]==0)
{
s=s+(char)i;
}
}
if(s.length()==0)
{
return "-1";
}
else
{
return s;
}
}
Comments
Post a Comment