DEV Community

hwangs12
hwangs12

Posted on

4 3

2022.05.15 Programming

Anagram

/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
var findAnagrams = function(s, p) {
    // let's create hashmap for p
    let champ = {}
    for (let pChar of p) {
        champ[pChar] ? champ[pChar]++ : champ[pChar] = 1
    }
    let right = 0
    let left = 0
    let count = p.length
    let output = new Array()

    while (right < s.length) {
        if (champ[s[right]] > 0) {
            count--
        }
        champ[s[right]]--
        right++

        if (count === 0) output.push(left)

        if (right - left === p.length) {
            if (champ[s[left]] >= 0) {
                count++

            }
            champ[s[left]]++
            left++
        }
    }
    return output
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Jetbrains image

Don’t Become a Data Breach Headline

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. Is your CI/CD protected? Check out these nine practical tips to keep your CI/CD secure—without adding friction.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay