My solution to a “Coding Problem” that I came across.

You are given two strings, a ‘parent’ string and a ‘query’ string respectively. Your task is
to determine how many times the query string – or an anagram of the query string –
appears in the parent string.

Here’s my solution in Typescript,

anagramDetect(problem: string, query: string): number {
    if(problem == null || query  == null) {
        return 0;
    }
    if(query.length > problem.length) {
        return 0;
    }
    let n = 0
    let sortJoin = (a:string) => a.split('').sort().join('');
    let sortedQ = sortJoin(query);
    problem.split('').map((_, i) => {
        if (sortJoin(problem.substr(i, query.length)) == sortedQ) {
            n += 1;
        }
    });
    return n; 
}

Like the blog? Subscribe for updates

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store. 

https://mydaytodo.com/apps/

Also, if you can leave a review on the App Store or Google Play Store, that would help too.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *