Adjacent Spice Jars
A restaurant kitchen has one long shelf of spice jars, each labelled with a letter (c for cumin, p for paprika). Read left to right, the shelf is a string like "sppcsx".
A recipe card lists the jars it needs, like "cps". Can the chef grab them in one sweep, meaning a block of side-by-side jars that holds exactly the recipe's jars, in any order, with nothing extra?
Your task
Write hasSpiceBlock(shelf, recipe). Return true if some run of recipe.length consecutive characters in shelf has exactly the same letters as recipe, each the same number of times, in any order. Otherwise return false.
If recipe is longer than shelf, return false.
hasSpiceBlock(shelf: string, recipe: string) → bool
Examples
shelf = "sppcsx" recipe = "cps"
true
spp and ppc each have two p's. The block at positions 2 to 4, pcs, has one c, one p and one s, which is the recipe in a different order, so the answer is true.shelf = "paspca" recipe = "aap"
false
The recipe needs two a's and one p. The 3-jar blocks are pas, asp, spc, pca. None of them has two a's, because the shelf's two a's are too far apart.
Constraints
- 1 ≤ recipe.length ≤ 1,000
- 0 ≤ shelf.length ≤ 10,000
- shelf and recipe contain lowercase letters a-z only.
Start with brute force
For every starting position, sort the next m jars and compare with the sorted recipe.
Each sort costs about m log m. A 10,000-jar shelf and a 1,000-letter recipe give roughly 9,000 blocks × 10,000 steps, which is around 90 million operations, even though each block differs from the previous one by just two jars.
Hints
0 of 4 shown- Hint 1
The block must have exactly
recipe.lengthjars, 3 in the first example. How many 3-jar blocks does the shelf have, and how could you describe each one so order does not matter? - Hint 2
Describe a block by how many jars of each letter it has. It matches when its counts equal the recipe's.
- Hint 3
When the block slides one step right, exactly one jar enters and one leaves. Update the counts instead of recounting.
- Hint 4
Count the recipe's letters and the first
mjars (m= recipe length). Then slide: add the jar ati, remove the jar ati - m, and compare counts after every step. With 26 letters, each comparison is fixed work.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
"Same jars in any order" means "same count of each letter". Count the recipe's letters, and keep the counts of a window of exactly m jars as it slides along the shelf.
Trace shelf "sppcsx" and recipe "cps", which needs c 1, p 1, s 1:
- The first window is
spp, with s 1 and p 2. It has no c and one p too many, so it does not match. - Sliding right,
centers andsleaves. The windowppchas p 2 and c 1, so it does not match. - Sliding right again,
senters andpleaves. The windowpcshas p 1, c 1 and s 1, which equals the recipe, so the answer is true.
Each slide changes two counts, and comparing 26 counts is fixed work, however long the recipe is.
function hasSpiceBlock(shelf, recipe) {
const m = recipe.length;
if (m > shelf.length) return false;
const idx = (ch) => ch.charCodeAt(0) - 97;
const need = new Array(26).fill(0);
const have = new Array(26).fill(0);
for (let i = 0; i < m; i++) {
need[idx(recipe[i])]++;
have[idx(shelf[i])]++;
}
const same = () => need.every((n, k) => n === have[k]);
if (same()) return true;
for (let i = m; i < shelf.length; i++) {
have[idx(shelf[i])]++; // jar entering on the right
have[idx(shelf[i - m])]--; // jar leaving on the left
if (same()) return true;
}
return false;
}def has_spice_block(shelf, recipe):
m = len(recipe)
if m > len(shelf):
return False
need = [0] * 26
have = [0] * 26
for i in range(m):
need[ord(recipe[i]) - 97] += 1
have[ord(shelf[i]) - 97] += 1
if need == have:
return True
for i in range(m, len(shelf)):
have[ord(shelf[i]) - 97] += 1 # jar entering on the right
have[ord(shelf[i - m]) - 97] -= 1 # jar leaving on the left
if need == have:
return True
return FalseRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.