首页 > 其他 > 详细

leetcode@ [205] Isomorphic Strings

时间:2015-12-02 17:41:32      阅读:339      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/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.

 

技术分享
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if(s.empty() && t.empty()) return true;
        if(s.length() != t.length()) return false;
        
        map<char, char> s_t_hsh; s_t_hsh.clear();
        map<char, char> t_s_hsh; t_s_hsh.clear();
        map<char, char>::iterator p_s_t, p_t_s;
        
        for(int i=0;i<s.length();++i) {
            p_s_t = s_t_hsh.find(s[i]); p_t_s = t_s_hsh.find(t[i]);
            if(p_s_t != s_t_hsh.end() && p_s_t->second != t[i]) return false;
            else s_t_hsh.insert(pair<char,char> (s[i],t[i]));
            
            if(p_t_s != t_s_hsh.end() && p_t_s->second != s[i]) return false;
            else t_s_hsh.insert(pair<char,char> (t[i],s[i]));
        }
        
        return true;
    }
};
leetcode 205: Isomorphic Strings

 

leetcode@ [205] Isomorphic Strings

原文:http://www.cnblogs.com/fu11211129/p/5013339.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!