当前位置:网站首页>242. 有效的字母异位词
242. 有效的字母异位词
2022-07-01 03:23:00 【Sun_Sky_Sea】
242. 有效的字母异位词
原始题目链接:https://leetcode.cn/problems/valid-anagram/
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。
示例 1:
输入: s = “anagram”, t = “nagaram”
输出: true
示例 2:
输入: s = “rat”, t = “car”
输出: false
提示:
1 <= s.length, t.length <= 5 * 104
s 和 t 仅包含小写字母
解题思路:
统计字符串的字符个数,存放在字典中,再判断字典是否一样即可。
代码实现:
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
import collections
a = collections.defaultdict(int)
b = collections.defaultdict(int)
for c in s:
a[c] += 1
for c in t:
b[c] += 1
if a == b:
return True
return False
边栏推荐
猜你喜欢

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

File upload and download

小程序容器技术与物联网IoT的结合点

Edlines: a real time line segment detector with a false detection control

Learning notes for introduction to C language multithreaded programming

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

Ridge regression and lasso regression

Pytorch training deep learning network settings CUDA specified GPU visible

Cookie&Session

Binary tree god level traversal: Morris traversal
随机推荐
Jeecgboot output log, how to use @slf4j
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
Finally in promise
Pyramid scene parsing network [pspnet] thesis reading
【伸手党福利】JSONObject转String保留空字段
数组的includes( )
Ultimate dolls 2.0 | encapsulation of cloud native delivery
Detailed explanation of ES6 deconstruction grammar
Avalanche problem and the use of sentinel
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
在 C 中声明函数之前调用函数会发生什么?
torch. histc
Implement pow (x, n) function
Thread data sharing and security -threadlocal
Leetcode 128 longest continuous sequence (hash set)
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
文件上传下载
Blueprism registration, download and install -rpa Chapter 1
pytorch nn.AdaptiveAvgPool2d(1)
FCN全卷积网络理解及代码实现(来自pytorch官方实现)