Manafall Daily Quest

String Permutation Rank

Function Name: getPermutationRank

Description

Write a function that calculates the rank of a string permutation in lexicographically sorted order without actually generating all permutations. For example, in the sorted list of permutations of 'abc', the rank of 'abc' is 1, 'acb' is 2, and so on. The function should handle duplicates and handle upper and lowercase characters as different. You must not use external libraries or built-in permutation functions. The rank should start at 1.

Requirements

  • The function should take a single string parameter.
  • All permutations should be considered case-sensitive ('a' is different from 'A').
  • The function should return an integer representing the rank of the string's permutation.
  • If the string contains non-letter characters, the function should throw an error or return an invalid input message.
  • Optimize the function to avoid factorial time complexity if possible.
  • Additional functions can be created to help with the main task.

Examples

Given the string 'bac', getPermutationRank('bac') should return 3.Given the string 'Bac', getPermutationRank('Bac') should return 4, since 'Bac', 'Bca', 'aBc' are before it in lexicographic order.Given the string 'aabc', getPermutationRank('aabc') should return 1, as it's the first in sorted order with duplicates.

Links

https://en.wikipedia.org/wiki/Permutation#Permutations_of_multisetshttps://en.wikipedia.org/wiki/Lexicographical_order

Prime Factorization

Sun Oct 27 2024

Write a function `calculatePrimeFactors` that receives a single positive integer greater than 1 and returns an array of its prime factors.Prime factors are the prime numbers that multiply together to equal the given integer. Every number greater than 1 is either a prime itself or can be made by multiplying prime numbers together.The returned array should contain the prime factors sorted in ascending order. If the input number is prime, return an array with just that number as the only element.

Prev Quest

Merge Intervals

Tue Oct 29 2024

Write a function 'mergeIntervals' that takes an array of intervals where each interval is a pair of integers representing an inclusive range. The function should merge all overlapping intervals and return an array of the merged intervals. An interval [a, b] is said to overlap with another interval [c, d] if they have at least one number in common. The resulting intervals should be outputted in ascending order, sorted by their start values.The function should handle an empty array, and if no intervals overlap, it should return the intervals as they were.

Next Quest