242. Valid Anagram--LeetCode Record

Tong_hdj發表於2016-07-11

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.

class Solution {
    func isAnagram(s: String, _ t: String) -> Bool {

        if s.characters.count != t.characters.count {
            return false
        }

        return s.characters.sort() == t.characters.sort()
    }
}

Test Case

""
""
"anagram"
"nagaram"
"rat"
"car"
"rat"
"-car"

相關文章