โจทย์: กำหนด String s และ t ตรวจสอบว่าเป็น Anagram กันหรือไม่ (ตัวอักษรเหมือนกันหมด ต่างแค่ลำดับ)
ตัวอย่าง
s = "bite", t = "biet" → True
อัลกอริทึม
- ถ้าความยาวต่างกัน → False
- Sort ตัวอักษรทั้งสอง String
- เปรียบเทียบว่าเหมือนกันหรือไม่
โค้ดตัวอย่าง
def solve(s, t):
if len(s) != len(t):
return False
s = sorted(s)
t = sorted(t)
return s == t
s = "bite"
t = "biet"
print(solve(s, t))
Input / Output
Input: "bite", "biet" Output: True