205. Isomorphic Strings

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.


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

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

class Solution {
    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;
       return false;

发布了30 篇原创文章 · 获赞 3 · 访问量 3万+


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客