Wednesday 27 January 2016

Find whether two given strings are permutations of each other

Write a program to find whether two given strings are permutations of each other.  A string str1 is a permutation of str2 if all the characters in str1 appear the same number of times in str2 and str2 is of the same length as str1.


Input: Two strings S1 and S2
Output:
yes - if they satisfy given criteria
no - otherwise

Constraints:
1 <= len(S1), len(S2) <= 100.
Characters from ASCII range 0 to 127.

White space will not be given in the string.

Solution-:


#include<stdio.h> #include<string.h> int main() {int a,b,i,e=0,c[128]={0},d[128]={0}; char s1[100],s2[100]; scanf("%s",&s1); scanf("%s",&s2); a=strlen(s1); b=strlen(s2); if(a!=b) printf("no"); else { for(i=0;i<a;i++) { c[s1[i]]++; d[s2[i]]++; } for(i=0;i<128;i++) {if(c[i]==d[i]) e++; } if(e==128) printf("yes"); else printf("no"); } return 0;}

1 comment: