[leetcode] problem 205 – isomorphic strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

Example

No.1

Input: s = “egg”, t = “add”

Output: true

No.2

Input: s = “foo”, t = “bar”

Output: false

No.3

Input: s = “paper”, t = “title”

Output: true

Note

You may assume both s and t have the same length.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public boolean (String s, String t) {
int[] array1 = new int[256];
int[] array2 = new int[256];

for (int i = 1; i <= s.length(); i++) {
int idx1 = s.charAt(i - 1);
int idx2 = t.charAt(i - 1);

if (array1[idx1] != array2[idx2])
return false;

array1[idx1] = i;
array2[idx2] = i;
}

return true;
}