leetcode 205. 同构字符串 简单 字符串

题目:
在这里插入图片描述

分析:同构的意思是字符串结构相等,而字母不必相等,那么只要逐个字符判断上次出现的位置在两个字符串中是否相等即可

代码:

class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(s.length() != t.length()){
            return false;
        }
        int[] schar = new int[128];
        int[] tchar = new int[128];
        for(int i = 0; i < s.length(); i++){
            char sch = s.charAt(i);
            char tch = t.charAt(i);
            if(schar[sch] != tchar[tch]){
                return false;
            }
            //+1的原因是int数组初始值为0,与初始值避开
            schar[sch] = i+1;
            tchar[tch] = i+1;
        }
        return true;
    }
}

在这里插入图片描述
在这里插入图片描述

发布了134 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_40208575/article/details/105346247