-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathisomorphic.java
More file actions
40 lines (29 loc) · 1017 Bytes
/
isomorphic.java
File metadata and controls
40 lines (29 loc) · 1017 Bytes
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
class isomorphic {
static final int CHAR = 26;
static boolean isoMorphic(String str1, String str2)
{
int n = str1.length();
int m = str2.length();
if (n != m)
return false;
// For counting the previous appearances of character in both the strings
int[] countChars1 = new int[CHAR];
int[] countChars2 = new int[CHAR];
for (int i = 0; i < n; i++) {
countChars1[str1.charAt(i) - 'a']++;
countChars2[str2.charAt(i) - 'a']++;
if (countChars1[str1.charAt(i) - 'a']
!= countChars2[str2.charAt(i) - 'a']) {
return false;
}
}
return true;
}
public static void main(String[] args)
{
System.out.println(isoMorphic("aab", "xxy") ? 1
: 0);
System.out.println(isoMorphic("aab", "xyz") ? 1
: 0);
}
}