String - Letters comparison ID:207
Two string values S1 and S2 are passed as input. The program must check if both S1 and S2 contain the same unique set of letters and print YES or NO. Assume all the letters (alphabets) are in smaller case.
Boundary Conditions:
Length of S1 is from 2 to 100
Length of S2 is from 2 to 100
Input Format:
First line will contain the string value of S1
Second line will contain the string value of S2
Output Format:
YES or NO depending on if both S1 and S2 contain the same set of unique letters.
IMPORTANT:
Please note that the output is CASE SENSITIVE. Hence print YES or NO (instead of yes or no)
Sample Input/Output:
Example 1:
Input:
read
dear
Output:
YES
Explanation:
Both S1 and S2 are formed using the letters - a d e r
Example 2:
Input:
record
decoder
Output:
YES
Explanation:
Both S1 and S2 are formed using the letters - c d e o r
Example 3:
Input:
energy
synergy
Output:
NO
Explanation:
S2 has additional letters - s y in it.
PROGRAM IN C:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define CHAR_SIZE 26
void alphacheck(char *, int []);
void create(char *, int[]);
int main()
{
char str1[50], str2[50];
int a1[CHAR_SIZE] = {0}, a2[CHAR_SIZE] = {0}, i;
char str1_alpha[CHAR_SIZE], str2_alpha[CHAR_SIZE];
scanf("%s", str1);
scanf("%s", str2);
alphacheck(str1, a1);
alphacheck(str2, a2);
create(str1_alpha, a1);
create(str2_alpha, a2);
if (strcmp(str1_alpha, str2_alpha) == 0)
{
printf("YES");
}
else
{
printf("NO");
}
return 0;
}
void alphacheck(char *str, int a[])
{
int i, index;
for (i = 0; i < strlen(str); i++)
{
str[i] = tolower(str[i]);
index = str[i] - 'a';
if (!a[index])
{
a[index] = 1;
}
}
}
void create(char *str, int a[])
{
int i, j = 0;
for (i = 0; i < CHAR_SIZE; i++)
{
if (a[i])
{
str[j++] = i + 'a';
}
}
str[j] = '\0';
}
Comments
Post a Comment