星空网 > 软件开发 > Java

[LeetCode] Isomorphic Strings

 

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.

 

     个人觉得挺有意思的一道题。首先要理解到底啥才叫lsomorphic strings。归纳出其的特点。

     其实就是字母可以不相同,但是结构必须相同。就是说对于string s,其出现重复字母的index必须和string t,出现重复的字母的index相等而且数量也相同。

     搞清楚这个就可以用if statement来写判断了。

      代码如下。~

 

public class Solution {  public boolean isIsomorphic(String s, String t) {    if(s.length()!=t.length()) return false;    HashMap<Character,Integer> smap=new HashMap<>();    HashMap<Character,Integer> tmap=new HashMap<>();    for(int i=0;i<s.length();i++){      if(!smap.containsKey(s.charAt(i))){        if(tmap.containsKey(t.charAt(i))){          return false;        }      }else{        int index=smap.get(s.charAt(i));        if(!tmap.containsKey(t.charAt(i))||tmap.get(t.charAt(i))!=index){          return false;        }      }      smap.put(s.charAt(i),i);      tmap.put(t.charAt(i),i);    }    return true;  }}

 




原标题:[LeetCode] Isomorphic Strings

关键词:string

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

eBay宣布与CCG达成战略合作:https://www.kjdsnews.com/a/1551743.html
海外市场|时尚是个轮回,滑轮鞋Roller sneakers引爆海外市场:https://www.kjdsnews.com/a/1551744.html
快手川流计划解读:https://www.kjdsnews.com/a/1551745.html
牛人案例分享:搭建自己的Dropshipping无货源平台月赚 500w美刀:https://www.kjdsnews.com/a/1551746.html
从零开始,外贸邮件营销如何做?:https://www.kjdsnews.com/a/1551747.html
Shopee激活旺季增长力:推出大促百万激励计划,正式上线全托管:https://www.kjdsnews.com/a/1551748.html
优秀的亚马逊运营每天必做的二十件事:https://www.xlkjsw.com/news/92277.html
对赌嗜血!深圳大卖卖身之后……:https://www.kjdsnews.com/a/1842198.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流