Skip to content

Instantly share code, notes, and snippets.

@baurine
Last active August 29, 2015 14:26
Show Gist options
  • Save baurine/e50de466e88569445baf to your computer and use it in GitHub Desktop.
Save baurine/e50de466e88569445baf to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/isomorphic-strings/
public class Solution {
public boolean isIsomorphic(String s, String t) {
char[] source, target;
char srcChar, targetChar;
boolean isIsomorphic = true;
// init two hash table
source = new char[128];
target = new char[128];
for (int i = 0; i < 128; i++) {
source[i] = 0;
target[i] = 0;
}
int len = s.length();
for (int i = 0; i < len; i++) {
srcChar = s.charAt(i);
targetChar = t.charAt(i);
// whether the key is appeared the first time
if (source[srcChar] == 0) {
// whether the value has been mapped
if (target[targetChar] == 0) {
target[targetChar] = 1;
source[srcChar] = targetChar;
} else {
isIsomorphic = false;
break;
}
} else if (source[srcChar] != targetChar) {
isIsomorphic = false;
break;
}
}
return isIsomorphic;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment