Medley
ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise. Each letter in magazine can only be used once in ransomNote.
This is a problem which has a lot in common with others we have solved in earlier weeks. The core task is one you should have solved before.
You need to check whether the magazine contains enough of each letter to build the ransom note. Each letter in the magazine can only be used once.
This is a frequency counting problem. Count how many times each letter appears in the magazine. Then check that every letter the ransom note needs is available in sufficient quantity.
⚙ Knowledge Hub: Counting Frequenciesfalsetruep and q, find and print the modified Kaprekar numbers in the range between p and q, inclusive. If none exist in the range, print INVALID RANGE.
This one is straightforward but fiddly. You need to read the description very carefully. Earlier problems on manipulating digits of integers, and string manipulation in general, are relevant here.
The key detail is how you split the squared number: the right part must have the same number of digits as the original number. The left part is whatever is left over (and may be empty, in which case treat it as 0).
This is a string manipulation problem. For each number in the range, square it, convert the result to a string, then split it at the correct position. Convert both halves back to integers and check whether they sum to the original number.
n from p to qsq = n * n and convert it to a stringd, the number of digits in nsq so that the right part has exactly d digits. The left part is everything before thatleft + right == n, it is a modified Kaprekar numberINVALID RANGE if none were foundwords, return the maximum value of length(words[i]) * length(words[j]) where the two words do not share common letters. If no such two words exist, return 0.
For this one, think about how to quickly test whether two words have letters in common. Preprocessing helps speed this up.
You need to check every pair of words and find the pair with the largest product of lengths, but only if they share no letters at all.
Use bit manipulation to represent each word as a bitmask. There are only 26 lowercase letters, so each word's letter set fits in a single integer. Set bit i if the word contains the i-th letter of the alphabet.
Two words share no letters if and only if their bitmasks ANDed together equal zero.
⚙ Knowledge Hub: Bit Maskingi is set if the word contains that letter. Use mask |= 1 << (ch - 'a')i, j)masks[i] & masks[j] == 0, the words share no letterslen(words[i]) * len(words[j])n × m grid where rows are numbered 1 to n and columns 1 to m. There is a network of train tracks that run in straight horizontal lines along a row, from column c1 to column c2. Tracks on the same row may overlap. The mayor wants to place lampposts in cells not occupied by any track. Given the grid dimensions and the list of tracks, find the number of cells where lampposts can be placed.
This is a tough medium with an underlying theme of 2D spatial grids. The size can get very large (up to a billion cells in each direction), so you need to find a way of doing this without actually storing the grid.
Tracks on the same row can overlap, so you need to merge overlapping intervals before counting occupied cells.
This is an interval merging problem. Group tracks by row. For each row that has tracks, sort the intervals by start column and merge any that overlap. The total occupied cells in that row is the sum of merged interval lengths. Subtract from m to get free cells for that row.
Rows with no tracks are entirely free (m cells each). You will also need to use 64-bit integers in languages without big integers, since n * m can exceed 231.
n * m (using 64-bit arithmetic)c1m free cells, which is already accounted for in the initial n * m.