| 
 | 1 | +package com.leetcode.maps;  | 
 | 2 | + | 
 | 3 | +import java.util.HashMap;  | 
 | 4 | +import java.util.Map;  | 
 | 5 | + | 
 | 6 | +import static org.junit.jupiter.api.Assertions.assertFalse;  | 
 | 7 | +import static org.junit.jupiter.api.Assertions.assertTrue;  | 
 | 8 | + | 
 | 9 | +/**  | 
 | 10 | + * Level: Easy  | 
 | 11 | + * Link: https://leetcode.com/problems/isomorphic-strings/  | 
 | 12 | + * Description:  | 
 | 13 | + * Given two strings s and t, determine if they are isomorphic.  | 
 | 14 | + *  | 
 | 15 | + * Two strings are isomorphic if the characters in s can be replaced to get t.  | 
 | 16 | + *  | 
 | 17 | + * All occurrences of a character must be replaced with another character while preserving the order of characters. No  | 
 | 18 | + * two characters may map to the same character but a character may map to itself.  | 
 | 19 | + *  | 
 | 20 | + * Example 1:  | 
 | 21 | + * Input: s = "egg", t = "add"  | 
 | 22 | + * Output: true  | 
 | 23 | + *  | 
 | 24 | + * Example 2:  | 
 | 25 | + * Input: s = "foo", t = "bar"  | 
 | 26 | + * Output: false  | 
 | 27 | + *  | 
 | 28 | + * Example 3:  | 
 | 29 | + * Input: s = "paper", t = "title"  | 
 | 30 | + * Output: true  | 
 | 31 | + *  | 
 | 32 | + * Note:  | 
 | 33 | + * You may assume both s and t have the same length.  | 
 | 34 | + *  | 
 | 35 | + * @author rampatra  | 
 | 36 | + * @since 2019-08-11  | 
 | 37 | + */  | 
 | 38 | +public class IsomorphicStrings {  | 
 | 39 | + | 
 | 40 | +    /**  | 
 | 41 | +     * Time Complexity:  | 
 | 42 | +     * Space Complexity:  | 
 | 43 | +     * Runtime: <a href="https://leetcode.com/submissions/detail/250762080/">8 ms</a>.  | 
 | 44 | +     *  | 
 | 45 | +     * @param s  | 
 | 46 | +     * @param t  | 
 | 47 | +     * @return  | 
 | 48 | +     */  | 
 | 49 | +    public static boolean isIsomorphic(String s, String t) {  | 
 | 50 | + | 
 | 51 | +        Map<Character, Character> sToTCharMap = new HashMap<>();  | 
 | 52 | +        Map<Character, Character> tToSCharMap = new HashMap<>();  | 
 | 53 | + | 
 | 54 | +        for (int i = 0; i < s.length(); i++) {  | 
 | 55 | +            char chFromS = s.charAt(i);  | 
 | 56 | +            char chFromT = t.charAt(i);  | 
 | 57 | +            if (sToTCharMap.get(chFromS) == null && tToSCharMap.get(chFromT) == null) {  | 
 | 58 | +                sToTCharMap.put(chFromS, chFromT);  | 
 | 59 | +                tToSCharMap.put(chFromT, chFromS);  | 
 | 60 | +            }  | 
 | 61 | +            Character mappedChFromSToT = sToTCharMap.get(chFromS);  | 
 | 62 | +            if (mappedChFromSToT == null || mappedChFromSToT != chFromT) {  | 
 | 63 | +                return false;  | 
 | 64 | +            }  | 
 | 65 | +        }  | 
 | 66 | + | 
 | 67 | +        return true;  | 
 | 68 | +    }  | 
 | 69 | + | 
 | 70 | +    /**  | 
 | 71 | +     * Time Complexity:  | 
 | 72 | +     * Space Complexity:  | 
 | 73 | +     * Runtime: <a href="https://leetcode.com/submissions/detail/250761329/">3 ms</a>.  | 
 | 74 | +     *  | 
 | 75 | +     * @param s  | 
 | 76 | +     * @param t  | 
 | 77 | +     * @return  | 
 | 78 | +     */  | 
 | 79 | +    public static boolean isIsomorphicWithoutMaps(String s, String t) {  | 
 | 80 | +        int[] charMap = new int[512];  | 
 | 81 | +        for (int i = 0; i < s.length(); i++) {  | 
 | 82 | +            char chFromS = s.charAt(i);  | 
 | 83 | +            char chFromT = t.charAt(i);  | 
 | 84 | +            if (charMap[chFromS] != charMap[chFromT + 256]) {  | 
 | 85 | +                return false;  | 
 | 86 | +            }  | 
 | 87 | +            charMap[chFromS] = charMap[chFromT + 256] = i + 1;  | 
 | 88 | +        }  | 
 | 89 | + | 
 | 90 | +        return true;  | 
 | 91 | +    }  | 
 | 92 | + | 
 | 93 | +    public static void main(String[] args) {  | 
 | 94 | +        assertTrue(isIsomorphic("egg", "add"));  | 
 | 95 | +        assertFalse(isIsomorphic("foo", "bar"));  | 
 | 96 | +        assertTrue(isIsomorphic("paper", "title"));  | 
 | 97 | +        assertFalse(isIsomorphic("ab", "aa"));  | 
 | 98 | + | 
 | 99 | +        assertTrue(isIsomorphicWithoutMaps("egg", "add"));  | 
 | 100 | +        assertFalse(isIsomorphicWithoutMaps("foo", "bar"));  | 
 | 101 | +        assertTrue(isIsomorphicWithoutMaps("paper", "title"));  | 
 | 102 | +        assertFalse(isIsomorphicWithoutMaps("ab", "aa"));  | 
 | 103 | +    }  | 
 | 104 | +}  | 
0 commit comments