同构字符串 leetcode 205
题目大意:给定两个字符串,判断是否为同构的字符串,字符间的对应关系一样,例如 paper 于 title
解题思路:使用两个hashmap分别做映射,当新字符在两个map中出现了key但是value不是对应的那个字符的话,就返回false,否则返回true
class Solution {public boolean isIsomorphic(String s, String t) {Map<Character, Character> sh = new HashMap<>(), th = new HashMap<>();for (int i = 0; i < s.length(); i++) {char sc = s.charAt(i), tc = t.charAt(i);if (sh.containsKey(sc) && tc != sh.get(sc) ||th.containsKey(tc) && sc != th.get(tc)) {return false;} else {sh.put(sc, tc);th.put(tc, sc);}}return true;}
}