242. Valid Anagram
Easy

66298FavoriteShare

Given two strings s and , write a function to determine if t is an anagram of s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值,如果所给的 Unicode 字符超出了你的 Python 定义范围,则会引发一个 TypeError 的异常

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_l = len(s)
t_l = len(t)
dic = {}
for i in range(97,123):
dic[chr(i)] = [0,0]
if s_l != t_l:
return False
for i in s:
dic[i][0] += 1
for j in t:
dic[j][1] += 1
for j in t:
if dic[j][0] != dic[j][1]:
return False
return True

最优的方法

class Solution:
def isAnagram(self, s: str, t: str) -> bool:

'''
s_l = len(s)
t_l = len(t)
dic = {}
for i in range(97,123):
dic[chr(i)] = [0,0]
if s_l != t_l:
return False
for i in s:
dic[i][0] += 1
for j in t:
dic[j][1] += 1
for j in t:
if dic[j][0] != dic[j][1]:
return False'''
for i in string.ascii_lowercase:
if s.count(i) != t.count(i):
return False
return True

return True

leetcode 字谜的更多相关文章

  1. LeetCode OJ:Valid Anagram(有效字谜问题)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  2. 【一天一道LeetCode】#242. Valid Anagram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. LeetCode算法题-Find All Anagrams in a String(Java实现)

    这是悦乐书的第228次更新,第240篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第95题(顺位题号是438).给定一个字符串s和一个非空字符串p,找到s中p的字谜的所有 ...

  4. 049 Group Anagrams 字谜分组

    给定一个字符串数组,将相同字谜组合在一起.(字谜是指颠倒字母顺序而成的字)例如,给定 ["eat", "tea", "tan", " ...

  5. LeetCode第152场周赛(Java)

    这算是我第一次正式参加 LeetCode 的周赛吧.通过两道题.意料之中(通过上次模拟可以看出来).总的来说,脑袋还是不太灵光.想的有点慢.全球第一名 0:10:19 就全部通过...感觉我的智商被狠 ...

  6. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  9. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

随机推荐

  1. Centos7开机启动自己的脚本的方法

    在百度上可以找到好几种Linux开机启动各种服务的方法,在这里我写的是自己喜欢的方式. 博主是一个不怎么记事的人,有些配置在系统的目录下,配置了一次后就忘了,再也不想去系统的目录下找各种奇奇怪怪的目录 ...

  2. 使用shell解析脚本依赖关系,并自动补数

    将脚本依赖关系放到表中 使用shell解析脚本依赖关系,递归的计算各个脚本. #!/bin/bash # dm 补数 basepath=$(cd ``; pwd) cd $basepath sourc ...

  3. lambda 和 iterable

    Lambda 表达式 你可以使用 Lambda 表达式创建匿名函数,即没有名称的函数.lambda 表达式非常适合快速创建在代码中以后不会用到的函数.尤其对高阶函数或将其他函数作为参数的函数来说,非常 ...

  4. Hexo+Github博客最简教程-Dockerfile自动搭建

    闲谈 拿出你的气质,打开你的电脑,借你半小时搭建好属于你的hexo博客,小生用dockerfile自动帮你搭建好:你只需要在你的mac或linux或windows上提前把docker安装好,如何安装不 ...

  5. html--form表单

    <!-- form 标签 作用:收集并提交用户的信息 属性: id   表单的id,用于js获取表单 name 表单的名字,用于js获取表单 action 表单提交的地址 method 表单提交 ...

  6. 【perl】simpleHTTP

    类似Python SimpleHTTPServer #!/usr/bin/perl # https://metacpan.org/pod/HTTP::Server::Simple # https:// ...

  7. 学习excel的使用技巧复制一列文本成新列去重

    学习excel的使用技巧复制一列文本成新列去重 其实比较简单的技巧  知道了就会  不知道就比较麻烦 直接复制到一列 找到 数据选项 删除重复项

  8. IDL打包发布exe(包含ENVI环境)

    IDL利用make_rt函数打包发布exe时,输出路径不能包含中文!!!

  9. JWT,oAuth和SSO的讨论

    JWT,oAuth和SSO的讨论 背景 Single Sign On有很多成熟的方案.基于Session的服务常使用缓存Session信息在一个缓存服务上(例如redis)以实现SSO,每个微服务使用 ...

  10. request和response的常用方法

    一.request   1.setAttribute()在Request域中存储数据  2.setCharacterEncoding()设置请求参数的编码方式,只对post请求有效  3.getMet ...