leetcode---Isomorphic Strings

来源:互联网 发布:广东外语外贸大学知乎 编辑:程序博客网 时间:2024/06/03 18:53

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.

 

class Solution {public:    string trans(string &s, int n)    {        char m[128] = {0};        char tmp = '0';        for(int i=0; i<n; i++)        {            if(m[s[i]] == 0)                m[s[i]] = tmp++;            s[i] = m[s[i]];        }        return s;    }    bool isIsomorphic(string s, string t) {        int n1 = s.length();        int n2 = t.length();        if(n1 != n2)            return false;        if(trans(s, n1) != trans(t, n1))            return false;        return true;    }};


 

0 0
原创粉丝点击