-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL205.java
More file actions
42 lines (35 loc) · 1.12 KB
/
L205.java
File metadata and controls
42 lines (35 loc) · 1.12 KB
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
package com.liang.leetcode;
/**
* @ClassName L205
* @description isomorphic-strings
* @Author LiaNg
* @Date 2018/12/6
*/
public class L205 {
public static void main(String[] args) {
String s = "ab";
String t = "aa";
L205 l = new L205();
System.out.println(l.isIsomorphic(s, t));
}
/**
* 给定两个字符串 s 和 t,判断它们是否是同构的。
* 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
* 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
*/
public boolean isIsomorphic(String s, String t) {
if (s.length() == 0) {
return true;
}
char[] sChars = s.toCharArray();
char[] tChars = t.toCharArray();
int i = 0;
while (i < s.length()) {
if (s.indexOf(String.valueOf(sChars[i])) != t.indexOf(String.valueOf(tChars[i]))) {
return false;
}
i++;
}
return true;
}
}