Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

242. Valid Anagram

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

Example 1:

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

Example 2:

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

Note:
You may assume the string contains only lowercase alphabets.

anagram意思是數字字元都相同,我想排序後檢查是否一樣最直覺了

1
2
3
4
T:O(nlogn), S:O(n)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return sorted(s) == sorted(t)

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels