Manafall Daily Quest

Counting Anagrams

Function Name: countAnagrams

Description

Write a function `countAnagrams` that takes two parameters: a string `s` and a string `p`. The function should return the number of anagram substrings of `p` found in `s`. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, 'listen' and 'silent' are anagrams of each other.

Requirements

  • The function `countAnagrams` must accept two parameters: `s` (the main string) and `p` (the pattern string).
  • Both input strings will consist of lowercase alphabets only.
  • The function should return an integer representing the count of anagram substrings of `p` in `s`.
  • Consider overlapping substrings as distinct occurrences. For example, in 'abab', 'ab' occurs twice as an anagram.
  • The function should be able to handle large input strings efficiently.
  • Do not use any built-in functions for direct anagram checking or permutation generation.
  • Optimize the function to run in O(n) time complexity, where n is the length of the string `s`.

Examples

Example 1: countAnagrams('cbaebabacd', 'abc') returns 2.Example 2: countAnagrams('afdgzyxksldfm', 'xsk') returns 0.

Find The Town Judge

Wed Oct 16 2024

In a town, there are N people labeled from 1 to N. There is a rumor that one of these people is secretly the town judge.If the town judge exists, then:1. The town judge trusts nobody.2. Everybody (except for the town judge) trusts the town judge.3. There is exactly one person that satisfies properties 1 and 2.You are given an array `trust` where `trust[i] = [a, b]` represents that the person labeled `a` trusts the person labeled `b`.Write a function `findJudge` that finds the town judge's label.The function should take in the parameter `N`, the number of people, and an array of trust pairs, and should return the label of the town judge if they exist or `-1` if they do not.

Prev Quest

Merge Intervals

Fri Oct 18 2024

Write a function called `mergeIntervals` that takes an array of intervals where each interval is represented as an array of two integers, denoting the inclusive start and the inclusive end of an interval. The function should merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input. The output array should be sorted by ascending starting points of the intervals.

Next Quest