DEV Community

hamza72x
hamza72x

Posted on • Edited on

[Grind 169] 7. Valid Anagram

Problem Link: https://leetcode.com/problems/valid-anagram/

Solution:

func isAnagram(s string, t string) bool {
    var n1 = len(s)
    var n2 = len(t)

    if n1 != n2 {
        return false
    }

    var data1 = make(map[byte]int)
    var data2 = make(map[byte]int)

    for i := 0; i < n1; i++ {
        var c1 = s[i]
        var c2 = t[i]

        _, ok1 := data1[c1]
        _, ok2 := data2[c2]

        if ok1 {
            data1[c1]++
        } else {
            data1[c1] = 1
        }

        if ok2 {
            data2[c2]++
        } else {
            data2[c2] = 1
        }
    }

    for k := range data1 {
        if data1[k] != data2[k] {
           return false 
        }
    }

    return true
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay