记录上一次字符出现的索引位置,应该相等。
package com.oracle.test;
import org.testng.annotations.Test;
public class IsoMorphicStringTest {
public boolean isIsomorphic(String s, String t) {
char [] ch1 = s.toCharArray();
char [] ch2 = t.toCharArray();
int [] chs0 = new int[256];
int [] chs1 = new int[256];
int i = 0;
while(i<s.length()) {
int ch1Int = ch1[i];
int ch2Int = ch2[i];
if(chs0[ch1Int] != chs1[ch2Int]) {
return false;
}
chs0[ch1Int] = i+1;
chs1[ch2Int] = i+1;
i++;
}
return true;
}
@Test
void testIsoMorphicString() {
System.out.println(isIsomorphic("aa", "ab"));
}
}