LeetCode 205. Isomorphic Strings

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.

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

题意:判断两个字符串,是否是同构字符串

思路:弄两个数组,循环遍历字符串,判断如果对应位置映射的数字不同那么就return false,否则就给同一位置的字母写入同样的数字,这样就可以判断了。

class Solution {
    public boolean isIsomorphic(String s, String t) {
        int[] mapS = new int[128];
        int[] mapT = new int[128];
        for(int i = 0; i < s.length(); i++){
            if(mapS[s.charAt(i)] != mapT[t.charAt(i)])
                return false;
            mapS[s.charAt(i)] = mapT[t.charAt(i)] = i + 1;
        }
        return true;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容