今天選題的方式 不是按序 也不是偷懶拿之前寫的(會標記Review) 是 Pick One (應該是隨機選題吧) <- 如上圖
Description
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
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?
思路
英文必須好
anagram == 重組
整句翻下來是 字串t 是否為 字串s的重組
而 解法是 長度一樣 , 把字串放進list後,進行sort,再繼續判斷是否兩個list slist
,tlist
是否一模模沒兩樣,是則 回傳 True
不一樣 直接 回傳 False
正解
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
slist = []
tlist = []
if len(s)!=len(t):
return False
for i in range(len(s)):
slist.append(s[i])
tlist.append(t[i])
slist.sort()
tlist.sort()
for j in range(len(s)):
if slist[j]!=tlist[j]:
return False
return True
Result