Skip to content

Commit 7c4ec13

Browse files
committed
Isomorhpic 동등문자열 찾기
1 parent 3418746 commit 7c4ec13

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

IsommorphicString.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashMap;
2+
3+
public class IsommorphicString {
4+
public static void main(String[] args) {
5+
System.out.println(isIsomorphic("abdc", "baba"));
6+
}
7+
8+
// my solution
9+
public static boolean isIsomorphic(String s, String t) {
10+
HashMap<Character, Character> hashMap = new HashMap<>();
11+
12+
if (s.length() != t.length())
13+
return false;
14+
15+
for (int i = 0; i < s.length(); i++) {
16+
if (!hashMap.containsKey(s.charAt(i)) && !hashMap.containsValue(t.charAt(i)))
17+
hashMap.put(s.charAt(i), t.charAt(i));
18+
else {
19+
try {
20+
if (hashMap.get(s.charAt(i)) == t.charAt(i))
21+
continue;
22+
else
23+
return false;
24+
}
25+
// NullPointerException이 발생했다는 것은 key에 결국 스트링t와 맞는
26+
// char가 없는 것이므로 무조건 false를 반환한다.
27+
catch (NullPointerException e){
28+
return false;
29+
}
30+
31+
}
32+
}
33+
return true;
34+
}
35+
}

0 commit comments

Comments
 (0)