242. Valid Anagram [easy] (Python)
给定两个字符串 s 和 t,写一个函数判断 t 是否是 s 的一个 “anagram”(颠倒字母顺序构成的词)。
比如: s = “anagram”, t = “nagaram”,返回 true; s = “rat”, t = “car”,返回false。
备注:假定给定的字符串都只包含小写字母。
进一步:如果输入包含 unicode 字符怎么办?你要怎么调整你的代码来解决这样的问题?
class Solution(object):def isAnagram(self, s, t):""":type s: str:type t: str:rtype: bool"""return sorted(s) == sorted(t)
class Solution(object):def isAnagram(self ,s,t):return sorted(s) == sorted(t)
说明
这里其实利用Python的排序方便了很多,但要注意 sorted(s)
返回的实际上是字符数组,所以上面代码 sorted(s) == sorted(t)
的比较是数组比较而不是字符串比较。
这个算法对于含有 unicode 的字符串也是有效的。
242. Valid Anagram [easy] (Python)相关推荐
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- [LeetCode]: 242: Valid Anagram
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- [LeetCode] NO. 242 Valid Anagram
[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- [leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列
/*思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true.记住这个方法*/if (s.length()!=t.length())return false;int[] wor ...
- 242.判断一个字符串是否为另一个的乱序 Valid Anagram
错误1 "aa" "bb" static public bool IsAnagram(string s, string t) { int sLength = s ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
- 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 = & ...
- 算法-Valid Anagram
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48979767 Given two strings s an ...
- [LeetCode] Valid Anagram - 字符串排序比较系列
题目概述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- 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 ...
最新文章
- Python 线程----线程方法,线程事件,线程队列,线程池,GIL锁,协程,Greenlet
- python DBSCAN聚类例子
- Android studio之如何快速查看页面的布局
- c语言数据结构五子棋实验报告,数据结构课程设计-五子棋
- 车仪表台上的装饰_@云浮车主:车内装饰品摆放不当将被处罚
- 十条有用的 Go 技术
- oracler日期函数相差六个月_oracle中计算两个日期的相差天数、月数、年数、小时数、分钟数、秒数等...
- 接口测试——jemter生成HTML测试报告
- SDR与DDR的区别
- 警惕同学录联系信息等泄露!
- hive出现内存溢出_hive问题处理
- 微电网逆变器VF控制simulink仿真模型。 采用解耦的电压电流双闭环控制, 输出电压电流波形质量良好
- 大数据开发之数据仓库
- JAVA集成华为推送 服务端
- 小话设计模式(十三)职责链模式
- python----语句
- 如何做好软件测试计划工作?
- 【043】基于51单片机的篮球比赛积分计时系统Proteus仿真
- 高等代数 行列式(第2章)
- My_sum函数进化