205. Isomorphic Strings

来源:互联网 发布:有自己的域名凡科建站 编辑:程序博客网 时间:2024/05/14 14:00

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.


利用map 将两个数组映射成aabb这种格式,然后比较aabb是否等于aabb即可

class Solution {public:    bool isIsomorphic(string s, string t) {        map<char, char> m1;map<char, char> m2;int len = s.size();char x = 'a';char x2 = 'a';char table[128] = { 0 };for (int i = 0; i<s.length(); i++) {char c = s.at(i);char c2 = t.at(i);if (!m1[c]) {m1[c] = x++;}if (!m2[c2]){m2[c2] = x2++;}s[i] = m1[c];t[i] = m2[c2];}        if(s==t) return true;       else       return false;            }};





0 0
原创粉丝点击