Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
Subscribe to see which companies asked this question
解题分析:
此题的意思很容易理解,意思就是给出两个字符串,然后判断是不是有一个混排成另一个字符串的。
确定是混排的话返回 True, 不正确的话返回 False.
思路是先将字符串转化为 list 然后进行排序,最后在进行比较,如果是有效的话,绝对是相等的。
# -*- coding:utf-8 -*- __author__ = 'jiuzhang' class Solution(object): def isAnagram(self, s, t): s_list = [] t_list = [] if len(s) != len(t): return False else: for i in range(len(s)): s_list.append(s[i]) t_list.append(t[i]) s_list.sort() t_list.sort() flag = 0 for i in range(len(s_list)): if s_list[i] == t_list[i]: flag += 1 else: flag += 0 if flag == len(s_list): return True else: return False
时间: 2024-10-14 16:20:31