[LeetCode]Isomorphic Strings
省事用了一个hashmap,时间复杂度是o(n2)。要是用两个hashmap时间复杂度会变为o(n)
public class Solution { public boolean isIsomorphic(String s, String t) { if (s.length() != t.length()) { return false; } int length = s.length(); HashMap<Character, Character> map = new HashMap<Character, Character>(); for (int i = 0; i < length; i++) { if (map.containsKey(s.charAt(i)) || map.containsValue(t.charAt(i))) { if (!map.containsKey(s.charAt(i)) || t.charAt(i) != map.get(s.charAt(i))) { return false; } } else { map.put(s.charAt(i), t.charAt(i)); } } return true; } }