加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

LeetCode 205. Isomorphic Strings C++

发布时间:2020-12-16 09:14:20 所属栏目:百科 来源:网络整理
导读: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 ord

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 = t = 
Output: true"egg","add"

Example 2:

Input: s = t = 
Output: false"foo","bar"

Example 3:

Input: s = t = 
Output: true

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

"paper","title"
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;
    }
        
    
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读