[LeetCode] 205. Isomorphic Strings (C++)

版权声明:本文为博主原创文章,未经博主允许不得转载。@ceezyyy11 https://blog.csdn.net/ceezyyy11/article/details/88877586

[LeetCode] 205. Isomorphic Strings (C++)

Easy

Share
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.

Example 1:

Input: s = “egg”, t = “add”
Output: true

Example 2:

Input: s = “foo”, t = “bar”
Output: false

Example 3:

Input: s = “paper”, t = “title”
Output: true

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

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        /*
        Total number of Character in ASCII table is 256 (0 to 255)
        */
        int m1[256]={0},m2[256]={0};
        if(s.size()!=t.size()) {
            return false;
        }
        else {
            for(int j=0;j<s.size();j++) {
                if(m1[s.at(j)]!=m2[t.at(j)]) {
                    return false;
                }
                else {
                    /*
                    Appear together at the same place
                    */
                    m1[s.at(j)]=j+1;
                    m2[t.at(j)]=j+1;
                }
            }
        }
        return true;
    }
};


/*
Submission Detail:

Runtime: 12 ms, faster than 67.45% of C++ online submissions for Isomorphic Strings.
Memory Usage: 9 MB, less than 37.90% of C++ online submissions for Isomorphic Strings.

*/

猜你喜欢

转载自blog.csdn.net/ceezyyy11/article/details/88877586