[LeetCode]205. Isomorphic Strings

来源:互联网 发布:渔夫帽牌子知乎 编辑:程序博客网 时间:2024/05/16 12:11

https://leetcode.com/problems/isomorphic-strings/

判断两个给定字符串是否pattern相同


只要判断两个字符串的当前字符上一次出现的位置是否相同即可得到结果

public class Solution {    public boolean isIsomorphic(String s, String t) {        int[] arr1 = new int[256];        int[] arr2 = new int[256];        for (int i = 0; i < s.length(); i++) {            if (arr1[s.charAt(i)] != arr2[t.charAt(i)]) {                return false;            }            // 不能等于i,反例:aa&ab            arr1[s.charAt(i)] = i + 1;            arr2[t.charAt(i)] = i + 1;        }        return true;    }}


0 0