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;
    }
}

你可能感兴趣的:(LeetCode 205. Isomorphic Strings)