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. c#调用python代码

    c#调用python的方法比较多,比如ironpython,尽管不用安装python环境,可是不兼容python众多的包,也只更新到了python2,通过创建python进程这种方式可以很好的解决兼容 ...

  2. Mysql8.0导入数据时出错

    在Windows操作系统下,使用命令行将已经创建好的txt文件导入到mysql的pet表中. 出现ERROR 1148 (42000): The used command is not allowed ...

  3. Verilog风格

    Verilog HDL编写原则: 可移植性强(多用宏定义:少用嵌入代码中的常数,即Magic Number:使用头文件): 必要的注释,代码易读: 模块间耦合尽可能低: 变量名与宏的命名规则: 变量名 ...

  4. CSS ——padding

    css样式中使用padding(内边距)会将盒子撑开? 解决办法:在样式中添加box-sizing:border-box;

  5. Python Queue(队列)

    Queue模块实现了多生产者.多消费者队列.当必须在多个线程之间安全地交换信息时,它在线程编程中特别有用,实现了所有必需的锁定语义. 一.该模块实现了三种类型的队列,它们的区别仅在于检索条目的顺序: ...

  6. 把JavaScript对象转化成JSON对象

    js => jsonvar jsonVar = {key: value}var jsonString = JSON.stringify(jsonVar)  //将JS对象转换为JSON字符串va ...

  7. C# HtmlAgilityPack 爬虫框架

    这两天公司不是很忙,在某个网站看见别人爬虫出来的数据感觉很有兴趣就玩了一把,网上找了一个 HtmlAgilityPack 爬虫框架,用了一下感觉很不错 首先从Nuget上面更新Package:Html ...

  8. ef err

    InvalidCastException: The field of type jcz.DomainModels.Sex must be a string, array or ICollection ...

  9. 利用jvisualvm使用btrace进行线上调试案例

    用途:btrace主要用于线上调试.通过btrace,可在不改动代码的前提下,方便的发现以下问题: 1.定位性能慢的接口服务: 2.实时打印堆栈信息,定位死锁位置: 3.定位占用大量内存空间的代码块: ...

  10. PID控制器介绍

    在维基百科上查到的PID的介绍,收藏一下,慢慢看. https://zh.wikipedia.org/wiki/PID%E6%8E%A7%E5%88%B6%E5%99%A8#%E6%AF%94%E4% ...