MEDIUMasked at 1 company

Number of Strings Which Can Be Rearranged to Contain Substring

A medium-tier problem at 55% community acceptance, tagged with Math, Dynamic Programming, Combinatorics. Reported in interviews at Meesho and 0 others.

Founder's read

You're asked to count how many strings can be rearranged to contain a target substring. It sounds straightforward until you realize the combinatorial trap: you need to account for the fixed positions of the substring while permuting the remaining characters, then subtract duplicates caused by repeated letters. This problem tests whether you can combine counting principles with the math of permutations. Meesho has asked it. The acceptance rate sits around 55%, which means roughly half the candidates either miss the permutation logic or botch the duplicate-character math. If this pattern hits your live OA and you blank on the formula, StealthCoder surfaces the working solution in seconds.

Companies asking
1
Difficulty
MEDIUM
Acceptance
55%

Companies that ask "Number of Strings Which Can Be Rearranged to Contain Substring"

If this hits your live OA

Number of Strings Which Can Be Rearranged to Contain Substring is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.

Get StealthCoder
What this means

The trick is breaking the problem into stages: fix the substring at one position, count valid permutations of the remaining characters, then sum across all valid positions. The obvious first attempt is usually just n! divided by the product of factorials for repeated characters. That's close, but you're missing the constraint that the substring must appear contiguous, which locks down certain character slots and changes what you're actually permuting. Once you map out which characters are 'free' to arrange and which are locked into the substring, you apply the multinomial coefficient formula. Common pitfall: forgetting that the substring itself may contain duplicate letters, which affects both the fixed portion and the count of remaining characters. This is a pure combinatorics problem dressed as a string problem. If you haven't drilled multinomial coefficients recently, StealthCoder is your safety net during the live assessment.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Strings Which Can Be Rearranged to Contain Substring recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Strings Which Can Be Rearranged to Contain Substring interview FAQ

What makes this harder than a standard permutation problem?+

You're not permuting all n characters freely. The substring is locked into contiguous positions, which reduces the degrees of freedom. You must count valid substring placements, then for each placement, count permutations of the leftover characters using the multinomial coefficient, accounting for repeated letters in the remaining pool.

How do I handle duplicate characters correctly?+

Duplicates appear in two places: inside the substring itself (which is fixed) and in the remaining characters available for rearrangement. For the remaining pool, divide by the factorial of each letter's count. Don't accidentally double-count duplicates that span the substring boundary.

Is this still asked at the companies listed?+

Meesho has reportedly asked it. The acceptance rate around 55% suggests it's not extremely common across all platforms, but it's definitely in the rotation for mid-level roles that care about combinatorics and math fundamentals.

What's the time complexity and does it matter?+

Most solutions run in O(n) or O(n log n) depending on sorting and factorial precomputation. For strings under 100 characters, either approach passes. The real bottleneck is getting the math right, not the algorithm speed.

How does this relate to Dynamic Programming?+

Some solutions use DP to count valid placements or memoize factorial and modular inverse computations, especially if the answer must be returned modulo a prime. It's more of a math problem that can be optimized with DP caching than a pure DP problem.

Want the actual problem statement? View "Number of Strings Which Can Be Rearranged to Contain Substring" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.