-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIsomorphicStrings.java
46 lines (44 loc) · 1.55 KB
/
IsomorphicStrings.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
/*https://practice.geeksforgeeks.org/problems/isomorphic-strings-1587115620/1*/
/*https://leetcode.com/problems/isomorphic-strings/*/
class Solution
{
//Function to check if two strings are isomorphic.
public static boolean areIsomorphic(String str1,String str2)
{
// Your code here
int[] hash1 = new int[26], hash2 = new int[26];
int i, j, m = str1.length(), n = str2.length();
if (m != n) return false;
for (i = 0; i < m; ++i)
{
++hash1[str1.charAt(i)-'a'];
++hash2[str2.charAt(i)-'a'];
}
HashMap<Character,Character> map = new HashMap<Character,Character>();
for (i = 0; i < m; ++i)
{
if (map.containsKey(str1.charAt(i)) && (Character)map.get(str1.charAt(i)) != str2.charAt(i)) return false;
map.put(str1.charAt(i),str2.charAt(i));
if (hash1[str1.charAt(i)-'a'] != hash2[str2.charAt(i)-'a']) return false;
}
return true;
}
}
class Solution {
public boolean isIsomorphic(String s, String t) {
int[] sHash = new int[128], tHash = new int[128];
Arrays.fill(sHash,-1);
Arrays.fill(tHash,-1);
int i = 0, n = s.length();
char[] a = s.toCharArray();
char[] b = t.toCharArray();
for (i = 0; i < n; ++i)
{
if (sHash[a[i]] != -1 && sHash[a[i]] != b[i]) return false;
if (tHash[b[i]] != -1 && tHash[b[i]] != a[i]) return false;
sHash[a[i]] = b[i];
tHash[b[i]] = a[i];
}
return true;
}
}