首页 > 其他 > 详细

Leetcode 205 Isomorphic Strings

时间:2019-05-29 15:52:59      阅读:93      评论:0      收藏:0      [点我收藏+]

lc205 Isomorphic Strings

思路就是检测s中相同字母在不同位置映射到t中相应位置的字母是否相同

举例来说,egg adj,第一个g对应位置的字母是d,而第二个g对应位置的字母是j,返回false

 

怎么检查是否相同呢?

最容易想到的就是HashMap<Character, Character>

 

还有一种方法,可以用int[128],与前者直接检查映射字母的方法不同,这里检查映射位置,每次检查当前mapS[s[i]] ?= mapT[t[i]],即判断当前字母s[i]和字母t[i]映射的位置是否相同

 1 class Solution {
 2     public boolean isIsomorphic(String s, String t) {
 3         if(s.length() == 0 && t.length() == 0)
 4             return true;
 5         int[] mapS = new int[256];
 6         int[] mapT = new int[256];
 7         
 8         for(int i=0; i<s.length(); i++){
 9             if(mapS[s.charAt(i)] != mapT[t.charAt(i)])
10                 return false;
11             mapS[s.charAt(i)] = i + 1;
12             mapT[t.charAt(i)] = i + 1;
13         }
14         
15         
16         return true;
17         
18     }
19 }

 

Leetcode 205 Isomorphic Strings

原文:https://www.cnblogs.com/hwd9654/p/10944193.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!