Valid Anagram

Given two strings s and t , return true if t is an anagram of s*, and* false otherwise .
Input: s = "rat", t = "car" Output: false

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

I decided to tackle this question using harsh maps. Basically the hash is populated with the string s and its index. To check if a string is an anagram the algrothms checks to see if string t is in hash map and returns true. My solution isn't working, can some please assist me ?

func isAnagram(_ s: String, _ t: String) -> Bool {
       //anagram "nagaram"//
       //decalre hashmap
       // populate hasmap with s
       // check if string in t is in s
    var result = [Character:Int]()
    for (index,element) in s.enumerated(){
        result [element] = index
    for words in t {
        if result.keys.contains(words){
            return true
    return false

Is this a homework question?

No leet code

Try to debug isAnagram("ab", "a"). While I’m at it, here’s a few more ordered by increasing “difficulty”:

  • isAnagram("abc", "cba"),
  • isAnagram("abba", "aaba"),
  • isAnagram("aba", "aab").
1 Like

It returns true because the value of the string s is present in the hash map.

Eager to jump into high difficulty I see. That is a good spirit.

Now, since you've figured what it returns (true), maybe you want to look into how/why it does that. You wrote that it's because the value of s is in the hash map. That is not false, but hardly useful. Try this instead

  • What exactly is in hash map? Is it the actual string s? Is it all the possible anagram of s? Are they all characters of s?
  • What does the checking using hash map later tell you? Does it mean word is an anagram of s? Does it mean word is the same string as s? Does it mean s contains a character word somewhere in there?
  • Is it enough to tell whether t is an anagram of s after checking word?

You can also look into other cases if you'd like. More scenarios are usually a good thing. It helps making sure you're not missing something.

1 Like

Also, before jumping into coding, I'd suggest that you try using pen & paper. See what you are doing manually; what you are keeping track of, what you write down, look at, etc. Such a method can be helpful at times like this.

Thank you for the great breakdown, Well detailed. Since I cant use pen and paper in actual interview, what other ways can I record my thought process?

Hmm, I’m not sure about that. I hardly see any technical interviews that don’t have any writing tools. At the very least, you could write something on a whiteboard if needed.

There are also cases where you are asked about “simple” algorithms. Those simply require practice, I guess?

There are a lot of things wrong with your function. First of all, it doesn't make sense to create a dictionary (result) if you only read the keys (result.keys.contains(words)). You never read the values.

Secondly, the indices of the characters in the two words have no bearing on whether they are anagrams or not.

All your function does is test whether the two words have a single character in common.

What you need to test for is if the two words contain the same characters and the same number of each of the characters:

func isAnagram(_ s: String, _ t: String) -> Bool {
    // Each key in the dictionaries represents a character in the string.
    // The values represent the occurrences of the characters in the string.
    var sDictionary: [Character: Int] = [:]
    for character in s {
        sDictionary[character, default: 0] += 1
    var tDictionary: [Character: Int] = [:]
    for character in t {
        tDictionary[character, default: 0] += 1

    return sDictionary == tDictionary
1 Like

A Swift string is itself a Collection of Comparable elements, thus:

func isAnagram(_ s: String, _ t: String) -> Bool {
    s.sorted() == t.sorted()
Terms of Service

Privacy Policy

Cookie Policy