File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1+ // Solution1: HashMap
2+ // T: O(n) S: O(1) (only 256 character)
3+ class Solution {
4+ public boolean isIsomorphic (String s , String t ) {
5+ if (s == null || s == null ) return false ;
6+ if (s .length () != t .length ()) return false ;
7+ Map <Character , Character > map = new HashMap <>();
8+ Map <Character , Character > revMap = new HashMap <>();
9+ char [] sch = s .toCharArray ();
10+ char [] tch = t .toCharArray ();
11+ for (int i = 0 ; i < sch .length ; i ++) {
12+ if (map .containsKey (sch [i ])) {
13+ if (map .get (sch [i ]) != tch [i ]) return false ;
14+ } else {
15+ if (revMap .containsKey (tch [i ])) return false ;
16+ map .put (sch [i ], tch [i ]);
17+ revMap .put (tch [i ], sch [i ]);
18+ }
19+ }
20+ return true ;
21+ }
22+ }
23+
124//Own Solution: HashMap T: O(n) S: O(512)
225class Solution {
326 public boolean isIsomorphic (String s , String t ) {
You can’t perform that action at this time.
0 commit comments