Number of Strings Which Can Be Rearranged to Contain Substring - Problem
Problem: Given an integer
What makes a string "good"?
A string is considered good if we can rearrange its characters to form a new string that contains "leet" as a contiguous substring.
Examples:
•
•
•
Key Insight: A string can be rearranged to contain "leet" if and only if it has at least 2 'l's, 2 'e's, and 1 't'. The remaining positions can be filled with any lowercase letters.
Return the answer modulo
n, find how many strings of length n using only lowercase English letters can be rearranged to contain the substring "leet".What makes a string "good"?
A string is considered good if we can rearrange its characters to form a new string that contains "leet" as a contiguous substring.
Examples:
•
"lteer" → can be rearranged to "leetr" ✅•
"letl" → cannot form "leet" (missing one 'e') ❌•
"aleteb" → can be rearranged to "leetab" ✅Key Insight: A string can be rearranged to contain "leet" if and only if it has at least 2 'l's, 2 'e's, and 1 't'. The remaining positions can be filled with any lowercase letters.
Return the answer modulo
109 + 7. Input & Output
example_1.py — Basic Case
$
Input:
n = 4
›
Output:
12
💡 Note:
For n=4, we need at least 2 l's, 2 e's, and 1 t (total 5 letters). Since we only have 4 positions, we need exactly 2 l's, 2 e's, 1 t, but that requires 5 letters. However, we can have strings like "llet" (2 l's, 1 e, 1 t) that can't form "leet". The actual count requires careful combinatorial calculation.
example_2.py — Minimum Length
$
Input:
n = 7
›
Output:
3196
💡 Note:
With 7 positions, we have more flexibility. We need ≥2 l's, ≥2 e's, ≥1 t, leaving 2 positions for any letters. Using inclusion-exclusion principle, we subtract invalid combinations from 26^7.
example_3.py — Edge Case
$
Input:
n = 3
›
Output:
0
💡 Note:
With only 3 positions, it's impossible to have at least 2 l's, 2 e's, and 1 t (which requires minimum 5 letters). Therefore, no valid strings exist.
Constraints
- 1 ≤ n ≤ 105
- Answer should be returned modulo 109 + 7
- Only lowercase English letters are allowed in strings
Visualization
Tap to expand
Understanding the Visualization
1
Count Required Letters
We need ≥2 l's, ≥2 e's, ≥1 t to form 'leet'
2
Calculate Total Possibilities
Without constraints: 26^n possible strings
3
Subtract Invalid Cases
Use inclusion-exclusion to remove strings missing required letters
4
Get Final Count
Valid strings = Total - Invalid using set theory
Key Takeaway
🎯 Key Insight: Use inclusion-exclusion principle to count mathematically rather than generate all strings - this transforms an exponential problem into a constant-time calculation!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code