题解 | 字母异位词的长度 -哈希字典-

字母异位词的长度

https://www.nowcoder.com/practice/59426f49136349b0901cc1b70447bf4b

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @param c string字符串 
# @return int整型
#  出错位置: 最后统计时候累加的是s_list[i] 的值 
class Solution:
    def isCongruent(self , s: str, c: str) -> int:
        # write code here
        s_list=[0]*26
        c_list=[0]*26
        s_len=len(s)
        c_len=len(c)
        if s_len!=c_len:
            return -1 
        for i in  range(s_len):
            s_list[ord(s[i])-ord('a')]+=1

        for i in  range(c_len):
            c_list[ord(c[i])-ord('a')]+=1
        if sum(s_list) !=sum(c_list):
            return -1
        count =0
        print(s_list)
        print(c_list)
        for i in range(len(s_list)):
            if s_list[i]==0 and c_list[i]==0:
                continue
            if s_list[i]!= c_list[i]:
                return -1
            if s_list[i]==c_list[i] and  s_list[i] !=0 and c_list[i]!=0:
                count+=s_list[i]  # 累加次数  -
    
        return count 

#哈希表##刷算法题#
全部评论

相关推荐

07-23 12:04
门头沟学院 Java
现在是很缺人吗
码农索隆:缺分母,不缺分子,这样好作为炫耀的资本
点赞 评论 收藏
分享
07-09 20:50
门头沟学院 Java
码农索隆:1.教育背景和荣誉证书合二为一。 2.获奖项目理一遍,你做了什么,对你求职的岗位有什么帮助,没有就删掉。 3.技能特长和教育背景交换位置。 4.技能特长写的太差,上网上找简历参考。都不用问你别的,一个redis就能把你问住,写写你具体会redis哪些方面的知识。
点赞 评论 收藏
分享
彧未sr:查看图片
投递牧原集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务