forked from singhmayank980/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphic-Strings.java
More file actions
38 lines (36 loc) · 1.18 KB
/
Copy pathIsomorphic-Strings.java
File metadata and controls
38 lines (36 loc) · 1.18 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
import java.util.*;
class Isomorphic {
static Scanner sc=new Scanner(System.in);
boolean isIsomorphic(String str1 ,String str2){
if (str1.length()!= str2.length())
return false;
Map<Character,Character> m= new HashMap<>();
Set<Character> s=new HashSet<>();
for(int i=0;i<str1.length();i++){
if(m.containsKey(str1.charAt(i))){
if(m.get(str1.charAt(i))!=str2.charAt(i))
return false;
}
else{
if(s.contains(str2.charAt(i)))
return false;
m.put(str1.charAt(i),str2.charAt(i));
s.add(str2.charAt(i));
}
}
return true;
}
public static void main(String args[]) {
Isomorphic obj= new Isomorphic();
String m,n;
System.out.println("Enter first string");
m=sc.nextLine();
System.out.println("Enter second string");
n=sc.nextLine();
boolean res=obj.isIsomorphic(m, n);
if(res==true)
System.out.println("Entered 2 strings are isomorphic");
else
System.out.println("Entered 2 strings are not isomorphic");
}
}