LeetCode 205. Isomorphic Strings C++

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

ASCII码有256个,维护两个数组,长度256,两个数组初始值相同,然后遍历两个string

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int m1[256] = {0};//ascii码共有256个
        int m2[256] = {0};
      
        
        for(int i = 0; i < s.size(); i++){
            if(m1[s[i]] != m2[t[i]]) return false;//对应位置值不同,不符合
            
            m1[s[i]] = i + 1;
            m2[t[i]] = i + 1;
        }
        return true;
    }
        
    
};

 

上一篇:Go语言中的函数(一)


下一篇:json