DEV Community

Debesh P.
Debesh P.

Posted on

205. Isomorphic Strings | LeetCode | Top Interview 150 | Coding Questions

Problem Link

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


Detailed Step-by-Step Explanation

https://leetcode.com/problems/isomorphic-strings/solutions/7487492/most-optimal-solution-hashmap-detailed-e-7qqa


leetcode 205


Solution

class Solution {
    public boolean isIsomorphic(String s, String t) {

        if (s.length() != t.length()) {
            return false;
        }

        Map<Character, Character> st = new HashMap<>();
        Map<Character, Character> ts = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {
            char ss = s.charAt(i);
            char tt = t.charAt(i);

            if (st.containsKey(ss) && st.get(ss) != tt) return false;
            if (ts.containsKey(tt) && ts.get(tt) != ss) return false;

            st.put(ss, tt);
            ts.put(tt, ss);
        }

        return true;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)