<p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>

<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>

<ul>
	<li><code>0 &lt;= i &lt;= s.length - a.length</code></li>
	<li><code>s[i..(i + a.length - 1)] == a</code></li>
	<li>There exists an index <code>j</code> such that:
	<ul>
		<li><code>0 &lt;= j &lt;= s.length - b.length</code></li>
		<li><code>s[j..(j + b.length - 1)] == b</code></li>
		<li><code>|j - i| &lt;= k</code></li>
	</ul>
	</li>
</ul>

<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;isawsquirrelnearmysquirrelhouseohmy&quot;, a = &quot;my&quot;, b = &quot;squirrel&quot;, k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == &quot;my&quot; and there exists an index 4 with s[4..11] == &quot;squirrel&quot; and |16 - 4| &lt;= 15.
- The index 33 is beautiful as s[33..34] == &quot;my&quot; and there exists an index 18 with s[18..25] == &quot;squirrel&quot; and |33 - 18| &lt;= 15.
Thus we return [16,33] as the result.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcd&quot;, a = &quot;a&quot;, b = &quot;a&quot;, k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == &quot;a&quot; and there exists an index 0 with s[0..0] == &quot;a&quot; and |0 - 0| &lt;= 4.
Thus we return [0] as the result.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= s.length &lt;= 5 * 10<sup>5</sup></code></li>
	<li><code>1 &lt;= a.length, b.length &lt;= 5 * 10<sup>5</sup></code></li>
	<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>

class RabinKarp:
    def __init__(self, base=31, mod=1e9+7):
        self.base = base
        self.mod = mod
        self.power = 1
        self.value = 0

    def hash(self, s):
        for c in s:
            self.value = (self.value * self.base + ord(c)) % self.mod
            self.power = (self.power * self.base) % self.mod
        return self.value

    def roll(self, old, new):
        self.value = (self.value * self.base - ord(old) * self.power + ord(new)) % self.mod
        return self.value
    
class Solution:
    def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
        # Beautiful Indices:
        # - Start of 'a' substring within s
        # - Less than or equal to k distance from start of 'b' substring within s
        
        if len(a) > len(s) or len(b) > len(s): return []
        
        # s = "aaxyxyxyxyaaxyaaxyx", a = "x", b = "y", k = 8
        # i                *
        # j         *
        # ans = [2, 4, 6, 8]
        
        target_a, target_b = RabinKarp().hash(a), RabinKarp().hash(b)
        
        left, right = 0, 0
        hash_a, hash_b = RabinKarp(), RabinKarp()
        hash_a.hash(s[:len(a)])
        hash_b.hash(s[:len(b)])
        
        ans = []
        
        def approx_equal(i, s2):
            return s[i] == s2[0] and s[i + len(s2) - 1] == s2[-1]
        
        while True:
            while hash_a.value != target_a:
                left += 1
                if left + len(a) > len(s): return ans
                hash_a.roll(s[left-1], s[left+len(a)-1])
            
            while left - right > k or hash_b.value != target_b:
                right += 1
                
                if right + len(b) > len(s): return ans
                hash_b.roll(s[right-1], s[right+len(b)-1])
            
            if hash_a.value == target_a and hash_b.value == target_b and abs(right - left) <= k:
                if approx_equal(left, a) and approx_equal(right, b):
                    ans.append(left)
            
            left += 1
            if left + len(a) > len(s): return ans
            hash_a.roll(s[left-1], s[left+len(a)-1])
            
        return ans
1566