leetcode 字谜
66298FavoriteShare
Given two strings s and t , 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 字谜的更多相关文章
- 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 ...
- 【一天一道LeetCode】#242. Valid Anagram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- LeetCode算法题-Find All Anagrams in a String(Java实现)
这是悦乐书的第228次更新,第240篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第95题(顺位题号是438).给定一个字符串s和一个非空字符串p,找到s中p的字谜的所有 ...
- 049 Group Anagrams 字谜分组
给定一个字符串数组,将相同字谜组合在一起.(字谜是指颠倒字母顺序而成的字)例如,给定 ["eat", "tea", "tan", " ...
- LeetCode第152场周赛(Java)
这算是我第一次正式参加 LeetCode 的周赛吧.通过两道题.意料之中(通过上次模拟可以看出来).总的来说,脑袋还是不太灵光.想的有点慢.全球第一名 0:10:19 就全部通过...感觉我的智商被狠 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
随机推荐
- 基于STM8的ADC读取---STM8-第四章
1. 综诉 想学会如何在STM8上使用ADC这个功能,我们先得了解单片机中ADC究竟是什么. ADC是模拟信号转成数值信号,单片机只能识别TTL电平,其实就是 1 或者 0 ,但是如果我们给它一个3. ...
- SYM File
Structure Programme header Section Symbol table Programme header An executable or shared object fil ...
- 刘志梅201771010115.《面向对象程序设计(java)》第十六周学习总结
实验十六 线程技术 实验时间 2017-12-8 1.实验目的与要求 (1)当线程的run方法执行方法体中最后一条语句后,并经由执行return语句返回时,或者出现了在方法中没有捕获的异常时,线程将 ...
- xshell完美开源替代方案(Kitty+MTPuTTY并设置全局字体)
xshell是收费的,过了30天就不能用了.我们应该找一个开源的替代品.说实话windows平台没有什么可选的,就是putty.但是原生的putty不好用,记不住密码,又不支持多标签. Kitty是基 ...
- Highcharts绘制曲线图小结
Higcharts绘制曲线图很好用! 虽然说Highcharts官网有API 刚接触这个领域,学有心得,理解不到位之处希望大家多多指教! 项目绘制的曲线是:平均水位随时间的变化而改变的水情走势图. 主 ...
- ssh 公钥无秘登录问题
1. 验证服务启动,网络端口连接正常 可以使用nc,telnet,或者密码模式的ssh来验证 2. 验证ssh client端的配置正确 可以尝试登录另外一台主机, 或者本机自校验 3. 验证ssh ...
- Oracle 归档日志文件
今天数据群有人反应网站不能正常打开,经检查Oracle数据库远程连不上,提示信息:ORA-00257: archiver error. Connect internal only, until f ...
- 使用JavaScript制作页面特效2
1.Date对象的常用方法 setFullYear() setMonth() setDate() setHours() setMinutes() setSeconds() 定时函数 setTimeou ...
- spring boot 整合 云之讯 demo
---恢复内容开始--- package com.zhourong.controller; import org.apache.commons.lang3.RandomStringUtils; imp ...
- 编译pcre 报错 error: Invalid C++ compiler or C++ compiler flags
安装c++ 编译器:yum -y install gcc-c++ ,再次编译通过.