美文网首页
[刷题防痴呆] 0205 - 同构字符串 (Isomorphic

[刷题防痴呆] 0205 - 同构字符串 (Isomorphic

作者: 西出玉门东望长安 | 来源:发表于2021-12-20 01:24 被阅读0次

    题目地址

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

    题目描述

    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.
    
    Example 1:
    
    Input: s = "egg", t = "add"
    Output: true
    Example 2:
    
    Input: s = "foo", t = "bar"
    Output: false
    Example 3:
    
    Input: s = "paper", t = "title"
    Output: true
    Note:
    You may assume both s and t have the same length.
    
    

    思路

    hash. 可以用数组, 也可以用hashmap. 从s走到t, 再从t映射到s.

    关键点

    注意, ASCII的范围是0-255. 所以用数组做hash的话, init数组长度为256.

    代码

    • 语言支持:Java
    class Solution {
        public boolean isIsomorphic(String s, String t) {
            char[] sc = s.toCharArray();
            char[] tc = t.toCharArray();
            
            int[] map = new int[256];        
            for (int i = 0; i < sc.length; i++) {
                if (map[sc[i]] == 0) {
                    map[sc[i]] = tc[i];
                } else {
                    if (map[sc[i]] != tc[i]) {
                        return false;
                    }
                }
            }
            
            int[] map2 = new int[256];
            for (int i = 0; i < tc.length; i++) {
                if (map2[tc[i]] == 0) {
                    map2[tc[i]] = sc[i];
                } else {
                    if (map2[tc[i]] != sc[i]) {
                        return false;
                    }
                }
            }
            
            return true;
        }
    }
    
    // hashmap
    class Solution {
        public boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
            int n = s.length();
            Map<Character, Character> sMap = new HashMap<>();
            Map<Character, Character> tMap = new HashMap<>();
    
            for (int i = 0; i < n; i++) {
                char c1 = s.charAt(i);
                char c2 = t.charAt(i);
                if (sMap.containsKey(c1)) {
                    if (sMap.get(c1) != c2) {
                        return false;
                    }
                }
                if (tMap.containsKey(c2)) {
                    if (tMap.get(c2) != c1) {
                        return false;
                    }
                }
    
                sMap.put(c1, c2);
                tMap.put(c2, c1);
            }
    
            return true;
        }
    }
    

    相关文章

      网友评论

          本文标题:[刷题防痴呆] 0205 - 同构字符串 (Isomorphic

          本文链接:https://www.haomeiwen.com/subject/vlpefrtx.html