Leetcode Isomorphic Strings

来源:互联网 发布:极品飞车ola车数据 编辑:程序博客网 时间:2024/06/09 18:19
Given two strings s and t, determine if they are isomorphic.


Two strings are isomorphic if the characters in s can be replaced to get t.


All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.


For example,
Given "egg", "add", return true.


Given "foo", "bar", return false.


Given "paper", "title", return true.


Note:

You may assume both s and t have the same length.

这道题是判断两个单词是否同构。这里的同构的意思是,对于例子egg和add,若存在一个映射,e->a,g->d,单词egg和add同构。注意,不可以多个字符映射到同一个字符中去,比如aa和ab。因此对于这种映射,要有两个数组来记录。由于是字符,不会超过256个,因此可以用数组来记录这种映射。

bool isIsomorphic(char* s, char* t) {    int l1=strlen(s);int l2=strlen(t);if(l1!=l2)return false;char c[256]={0};char cc[256]={0};for(int i=0;i<l1;i++){if(c[s[i]]==0)c[s[i]]=t[i];else {if(c[s[i]]!=t[i])return false;}if(cc[t[i]]==0)cc[t[i]]=s[i];else if(cc[t[i]]!=s[i])return false;}return true;}


0 0
原创粉丝点击