Isomorphic Strings

,


Problem

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

1
2
Input: s = "egg", t = "add"
Output: true

Example 2

1
2
Input: s = "foo", t = "bar"
Output: false

Example 3

1
2
Input: s = "paper", t = "title"
Output: true

Note

  • You may assume both s and t have the same length.

My Answer

  • HashMap을 이용하자. HashMap의 키는 s의 문자, 값은 t의 문자를 넣자.
  • s를 순회 하면서, 문자가 HashMap의 키로 포함되어 있는지 확인하자.
  • 만약 키로 포함되어 있으면서, 값이 t의 문자와 다르다면 false
  • 만약 키로 포함되어 있지 않은데, t의 문자가 값으로 HashMap에 포함되어 있다면 false
  • 위 조건을 만족하지 않는다면, HashMap에 넣고 다음 순회.
  • 반복해서 돌았는데, false가 발생하지 않는다면, true이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public boolean isIsomorphic(String s, String t) {
        Map<Character, Character> hashmap = new HashMap<>();
        
        for(int i=0;i<s.length(); i++) {
            char s_c = s.charAt(i);
            
            if ( hashmap.containsKey(s_c) ) {
                if ( hashmap.get(s_c) != t.charAt(i)) return false;
            } else {
                if ( hashmap.containsValue(t.charAt(i))) return false;
                
                hashmap.put(s_c, t.charAt(i));               
            }             
        }
        
        return true;
    }
}