-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsIsomorphic.java
More file actions
47 lines (37 loc) · 1.07 KB
/
IsIsomorphic.java
File metadata and controls
47 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Arrays;
import java.util.HashMap;
public class IsIsomorphic{
public static boolean isIsomorphic(String s, String t){
if(s.length() != t.length())
return false;
if(s.length() == 0 && t.length() == 0)
return true;
HashMap<Character, Character> map = new HashMap<Character, Character>();
for(int i=0; i < s.length(); i++){
char c1 = s.charAt(i);
char c2 = t.charAt(i);
if(map.containsKey(c1)){
if(c2 != map.get(c1))
return false;
}
else
map.put(c1,c2);
}
return true;
}
public static void main(String[] args){
if(args.length < 2){
System.out.println("usage : java <program> <string1> <string2>");
}
else{
System.out.println("This program checks if two given string s and t are isomorphic.\n Two Strings are isomorphic if the characters in s can be replaced to get t");
String s = args[0];
String t = args[1];
boolean result = isIsomorphic(s,t);
if(result)
System.out.println(s + " and " + t + " are isomorphic");
else
System.out.println(s + " and " + t + " are not isomorphic");
}
}
}