做网站前端设计需要哪些证书,做动图的网站,小店网站怎么做,六枝特区企业网络推广如何做205. 同构字符串 Leetcode 205. 同构字符串 一、题目描述二、我的想法三、其他人的题解 一、题目描述
给定两个字符串 s 和 t #xff0c;判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 t #xff0c;那么这两个字符串是同构的。
每个出现的字符都应…205. 同构字符串 Leetcode 205. 同构字符串 一、题目描述二、我的想法三、其他人的题解 一、题目描述
给定两个字符串 s 和 t 判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 t 那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符同时不改变字符的顺序。不同字符不能映射到同一个字符上相同字符只能映射到同一个字符上字符可以映射到自己本身。
示例 1: 输入s “egg”, t “add” 输出true
示例 2 输入s “foo”, t “bar” 输出false
示例 3 输入s “paper”, t “title” 输出true
提示
1 s.length 5 * 10^4t.length s.lengths 和 t 由任意有效的 ASCII 字符组成
二、我的想法
1.最开始想的是将每个字符的出现次数都放在 defaultdict 中如果这两个字符串所得的次数列表相等那就说明这俩可以转换。但是没过去37 / 46 个通过的测试用例。没过的那个测试用例为s “bbbaaaba”, t “aaabbbba”。
class Solution:def isIsomorphic(self, s: str, t: str) - bool:sdict defaultdict(int)tdict defaultdict(int)strLen len(s)for i in range(strLen):sdict[s[i]] 1tdict[t[i]] 1slist list(sdict.values())tlist list(tdict.values())slist.sort()tlist.sort()return slisttlist2.又想了想应该是字符与字符之间是对应关系。设置一个 dict 遍历字符串如果 s 对应的字符在 dict 中判断对应的 t 的值是否与 dict 中的 value 值相等如果不相等的话直接就返回 False 。如果直到遍历完字符串还没返回的话就返回 True。
class Solution:def isIsomorphic(self, s: str, t: str) - bool:compareDict dict()sLen len(s)for i in range(sLen):if s[i] not in compareDict:compareDict[s[i]] t[i]else:if compareDict[s[i]] ! t[i]:return Falsereturn True3.结果又没过38 / 46 个通过的测试用例整半天就多通过了一个测试用例。想了想又加个判断条件如果 s 对应的字符没在 dict 中但是 t 对应的字符在 dict 中返回 False。于是测试通过。
class Solution:def isIsomorphic(self, s: str, t: str) - bool:compareDict dict()sLen len(s)for i in range(sLen):if s[i] not in compareDict :if t[i] in compareDict.values():return FalsecompareDict[s[i]] t[i]else:if compareDict[s[i]] ! t[i]:return Falsereturn True三、其他人的题解
看了一下大家的做法大概都是双向哈希表。
class Solution:def isIsomorphic(self, s: str, t: str) - bool:mp1, mp2 {}, {}for a, b in zip(s, t):if a in mp1 and mp1[a] ! b:return Falseif b in mp2 and mp2[b] ! a:return Falsemp1[a] bmp2[b] areturn True作者Benhao
链接https://leetcode.cn/problems/isomorphic-strings/solutions/1/python-zheng-fan-ha-xi-biao-ying-she-by-0si7q/
来源力扣LeetCode
著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。其中 zip 函数的用法 源码zip([iterable, …])iterable为迭代器可以用列表、元组、字典、集合等。 主要功能将其迭代器中的多个序列压缩成zip对象或者列表版本不一样返回元素不一样但其构成元素都是元组
来自于 码农研究僧的 Python zip函数 详解全 文章转载自: http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.ksjmt.cn.gov.cn.ksjmt.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.hcsnk.cn.gov.cn.hcsnk.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.nstml.cn.gov.cn.nstml.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn http://www.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn