国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] Isomorphic Strings

2019-11-15 01:16:23
字體:
來源:轉載
供稿:網友
[LeetCode] Isomorphic Strings

Given two stringssandt, determine if they are isomorphic.

Two strings are isomorphic if the characters inscan be replaced to gett.

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] Swap Nodes in Pairs

下一篇:Plus One

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奉节县| 常德市| 九寨沟县| 纳雍县| 徐闻县| 乌审旗| 奎屯市| 长垣县| 正蓝旗| 临武县| 芮城县| 藁城市| 永德县| 梅河口市| 眉山市| 拜城县| 旬邑县| 汕尾市| 内乡县| 弋阳县| 岳西县| 峡江县| 奉节县| 建德市| 沁源县| 隆回县| 崇礼县| 新津县| 镇安县| 永昌县| 旬邑县| 越西县| 五家渠市| 陆河县| 莱州市| 甘南县| 边坝县| 博爱县| 达尔| 井冈山市| 邯郸市|