-
Notifications
You must be signed in to change notification settings - Fork 57
/
Isomorphic.java
62 lines (54 loc) · 1.77 KB
/
Isomorphic.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//Program to check whether two strings s and t are isomorphic or not
//Two strings are isomorphic if the characters in s can be replaced to get t
//for example, "egg" and "add" are isomorphic but "foo" and "bar" are not
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Isomorphic {
static boolean isIsomorphic(String s,String t)
{
if(s==null || t==null)
return false;
if(s.length()!=t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
HashMap<Character,Character> map = new HashMap<>();
for(int i=0;i<s.length();i++)
{
char c1=s.charAt(i);
char c2=t.charAt(i);
Character c = getKey(map,c2);
if(c!=null && c!=c1) {
return false;
}
else if(map.containsKey(c1)) {
if(c2!=map.get(c1))
return false;
}
else {
map.put(c1,c2);
}
}
return true;
}
static Character getKey(HashMap<Character,Character> map, Character target)
{
for(Map.Entry<Character,Character> entry : map.entrySet())
{
if(entry.getValue().equals(target))
return entry.getKey();
}
return null;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Two Strings: ");
String s1=sc.next();
String s2 = sc.next();
if(isIsomorphic(s1,s2))
System.out.println("Strings are isomorphic");
else
System.out.println("Strings are not isomorphic");
}
}