You are given a 0-indexed string s, two pattern strings a and b, and an integer k representing the maximum allowed distance.
Your task is to find all "beautiful indices" in the string. An index i is considered beautiful if:
- The substring starting at index
imatches patternaexactly - There exists at least one index
jwhere the substring starting atjmatches patternbexactly - The distance between indices
iandjis at mostk(i.e.,|i - j| β€ k)
Goal: Return all beautiful indices in sorted ascending order.
Example: If s = "isawsquirrelnearmyhouse", a = "saw", b = "house", and k = 15, then index 2 is beautiful because s[2:5] = "saw" matches pattern a, and s[19:24] = "house" matches pattern b, with distance |19 - 2| = 17 β€ 15 being false, but there might be other valid j positions.
Input & Output
Visualization
Time & Space Complexity
O(nΒ²) to check all pairs of positions, O(n) for substring comparison in worst case
Only storing the result array, no additional data structures
Constraints
- 1 β€ s.length β€ 105
- 1 β€ a.length, b.length β€ 10
- 0 β€ k β€ s.length
- s, a, and b consist only of lowercase English letters