码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】205. Isomorphic Strings 解题小结

时间:2016-08-30 00:04:09      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

题目: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.

设置两个cs和ct数组,记录目前遍历字符串s和t的下标i+1,如果两者不相等,返回false。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int cs[255] = {0}, ct[255] = {0};
        for (int i = 0; i < s.size(); ++i){
            if (cs[s[i]] != ct[t[i]])
                return false;
            else{
                cs[s[i]] = i + 1;
                ct[t[i]] = i + 1;
            }
        }
        return true;
    }
};

 

【LeetCode】205. Isomorphic Strings 解题小结

标签:

原文地址:http://www.cnblogs.com/Doctengineer/p/5820226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!