Sunday, November 8, 2015

Check if two given strings are isomorphic

#include <string>
#include <iostream>
using namespace std;
int MAX_CHARS=256;
int isomer(string a,string b)
{
int n=a.length();
int m=b.length();
if(n!=m)
return 0;
int map2[MAX_CHARS];
bool map1[256]={false};
int i=0;
while(i<n)
{
if(map1[a[i]]&&b[i]!=map2[a[i]])
return 0;
if(!map1[a[i]])
{
map1[a[i]]=true;
map2[a[i]]=b[i];
}
i++;
}
return 1;
}
int main()
{
string s="abca",s1="xxzz";

cout<<isomer(s,s1);
}

No comments:

Post a Comment

Contributors

Translate