Two String Problem
Two Strings A real Hackerearth Problem
problem (Reffer to )
Given two strings of equal length, you have to tell whether they both strings are identical.
Two strings S1 and S2 are said to be identical, if any of the permutation of string S1 is equal to the string S2. See Sample explanation for more details.
Input :
- First line, contains an intger 'T' denoting no. of test cases.
- Each test consists of a single line, containing two space separated strings S1 and S2 of equal length.
Output:
- For each test case, if any of the permutation of string S1 is equal to the string S2 print YES else print NO.
Constraints:
- 1<= T <=100
- 1<= |S1| = |S2| <= 10^5
- String is made up of lower case letters only.
Note : Use Hashing Concept Only . Try to do it in O(string length) .
YES YES NO
//code in C++
#include <iostream>using namespace std;int main(){int T; cin>>T;while(T--){string s1,s2; cin>>s1>>s2;// declare array for s1 and s2;// for s1int a[26]={0};// for s2int b[26]={0};// length of stringint k = (int)s1.size();for(int i=0;i<k;i++){int x = s1[i]-97; // In c++ automatic data type conversion in char to inta[x]++;int y = s2[i]-97;b[y]++;}// we declare countint count=0;// we got completer array a and bfor(int j=0;j<26;j++){if(a[j]!=b[j])count++;}if(count==0)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}}
Comments
Post a Comment