LeetCode 205. Isomorphic Strings

xiaoxiao2021-02-28  28

同构字符串

public boolean isIsomorphic1(String s, String t) { if(s.length() != t.length()) return false; char[] chS = s.toCharArray(); char[] chT = t.toCharArray(); int[] mapS = new int[128]; int[] mapT = new int[128]; int len = chS.length; for(int i = 0; i < len; i++) { //如果当前位置的字符上一次出现索引不同 if(mapS[chS[i]] != mapT[chT[i]]) { return false; } //纪录字符这次出现的索引 mapS[chS[i]] = i + 1; mapT[chT[i]] = i + 1; } return true; }

一般的题目都是用int[] map纪录字符串的出现次数,这题纪录字符上一次出现的索引。从而达到纪录结构的目的。

转载请注明原文地址: https://www.6miu.com/read-2600078.html

最新回复(0)