-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsomorphicStrings.java
More file actions
60 lines (49 loc) · 1.56 KB
/
Copy pathIsomorphicStrings.java
File metadata and controls
60 lines (49 loc) · 1.56 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
48
49
50
51
52
53
54
55
56
57
58
59
60
package String;
import java.util.*;
class IsomorphicStrings {
public boolean isIsomorphic(String s, String t) {
// Base code
if (s == null || s.length() <= 1)
return true;
HashMap<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
char b = t.charAt(i);
if (map.containsKey(a)) {
if (map.get(a).equals(b))
continue;
else
return false;
} else {
if (!map.containsValue(b))
map.put(a, b);
else
return false;
}
}
return true;
}
public static void main(String[] args) {
IsomorphicString obj = new IsomorphicString();
if(obj.isIsomorphic("xxy", "aab") == true)
System.out.println("ARE ISOMORPHIC");
else
System.out.println("NOT");
}
/* More better approach
Map<Character,Character> map=new HashMap<>();
Set<Character> set=new HashSet<>();
if(s==null)
return true;
for(int i=0;i<s.length();i++){
if(map.containsKey(s.charAt(i))){
if(map.get(s.charAt(i))!=t.charAt(i)) return false;
} else{
if(set.contains(t.charAt(i))) return false;
map.put(s.charAt(i),t.charAt(i));
set.add(t.charAt(i));
}
}
return true;
*/
}