Isomorphic Strings

来源:互联网 发布:全国网络诈骗报警平台 编辑:程序博客网 时间:2024/05/16 13:50
题目:

1、s中同一个字符要映射到t中的同一个字符;

2、s中通一个字符不能映射到t中的同一个字符——》t中的同一个字符要映射到s中的同一个字符

so需要s到t判断一下,t到s再判断一下;

public class Solution {    public boolean isIsomorphic(String s, String t) {        if (s.length() != t.length()) {return false;}        return isSingleIsomorphic(s,t) && isSingleIsomorphic(t, s);    }private boolean isSingleIsomorphic(String s, String t) {// TODO Auto-generated method stubHashMap<Character, Character> map = new HashMap<Character, Character>();char ch1, ch2;        for (int i = 0; i < s.length(); i++) {ch1 = s.charAt(i);ch2 = t.charAt(i);if (!map.containsKey(ch1)) {map.put(ch1, ch2);}else {if (map.get(ch1) != ch2) {return false;}}}        return true;}}


0 0
原创粉丝点击