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

字母异位词的长度

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 

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

相关推荐

这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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