<p>You are given a <strong>0-indexed</strong> string <code>s</code>. You are also given a <strong>0-indexed</strong> string <code>queryCharacters</code> of length <code>k</code> and a <strong>0-indexed</strong> array of integer <strong>indices</strong> <code>queryIndices</code> of length <code>k</code>, both of which are used to describe <code>k</code> queries.</p>

<p>The <code>i<sup>th</sup></code> query updates the character in <code>s</code> at index <code>queryIndices[i]</code> to the character <code>queryCharacters[i]</code>.</p>

<p>Return <em>an array</em> <code>lengths</code> <em>of length </em><code>k</code><em> where</em> <code>lengths[i]</code> <em>is the <strong>length</strong> of the <strong>longest substring</strong> of </em><code>s</code><em> consisting of <strong>only one repeating</strong> character <strong>after</strong> the</em> <code>i<sup>th</sup></code> <em>query</em><em> is performed.</em></p>

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

<pre>
<strong>Input:</strong> s = &quot;babacc&quot;, queryCharacters = &quot;bcb&quot;, queryIndices = [1,3,3]
<strong>Output:</strong> [3,3,4]
<strong>Explanation:</strong> 
- 1<sup>st</sup> query updates s = &quot;<u>b<strong>b</strong>b</u>acc&quot;. The longest substring consisting of one repeating character is &quot;bbb&quot; with length 3.
- 2<sup>nd</sup> query updates s = &quot;bbb<u><strong>c</strong>cc</u>&quot;. 
  The longest substring consisting of one repeating character can be &quot;bbb&quot; or &quot;ccc&quot; with length 3.
- 3<sup>rd</sup> query updates s = &quot;<u>bbb<strong>b</strong></u>cc&quot;. The longest substring consisting of one repeating character is &quot;bbbb&quot; with length 4.
Thus, we return [3,3,4].
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abyzz&quot;, queryCharacters = &quot;aa&quot;, queryIndices = [2,1]
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong>
- 1<sup>st</sup> query updates s = &quot;ab<strong>a</strong><u>zz</u>&quot;. The longest substring consisting of one repeating character is &quot;zz&quot; with length 2.
- 2<sup>nd</sup> query updates s = &quot;<u>a<strong>a</strong>a</u>zz&quot;. The longest substring consisting of one repeating character is &quot;aaa&quot; with length 3.
Thus, we return [2,3].
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
	<li><code>k == queryCharacters.length == queryIndices.length</code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
	<li><code>queryCharacters</code> consists of lowercase English letters.</li>
	<li><code>0 &lt;= queryIndices[i] &lt; s.length</code></li>
</ul>

from sortedcontainers import SortedList

class Solution:
    def longestRepeating(self, s: str, queryCharacters: str, queryIndices: List[int]) -> List[int]:
        
        s = list(s)
        loc = defaultdict(lambda: SortedList()) 
        max_len = SortedList() 

        i = 0
        while i < len(s):
            j = i
            while j < len(s) and s[i] == s[j]:
                j += 1
            loc[s[i]].add((i, j-1))
            max_len.add(j - i)
            i = j
            
        def del_seg(w, old_l, old_r):
            loc[w].remove((old_l, old_r))
            max_len.remove(old_r - old_l + 1)
        
        def add_seg(w, new_l, new_r):
            loc[w].add((new_l, new_r))
            max_len.add(new_r - new_l + 1)

        ans = [0] * len(queryIndices)
        for k in range(len(queryIndices)):
            old, new = s[queryIndices[k]], queryCharacters[k]
            s[queryIndices[k]] = new
            pos = (queryIndices[k], queryIndices[k])

            if pos in loc[old]:
                del_seg(old, *pos)
            else:
                l1 = bisect.bisect_left(loc[old], pos)
                if l1 == 0:
                    old_pos = loc[old][l1]
                    l, r = old_pos
                    del_seg(old, *old_pos)
                    add_seg(old, l + 1, r)
                elif l1 == len(loc[old]):
                    old_pos = loc[old][l1-1]
                    l, r = old_pos
                    del_seg(old, *old_pos)
                    if pos[0] == r:
                        add_seg(old, l, r-1)
                    else:
                        add_seg(old, l, pos[0]-1)
                        add_seg(old, pos[0] + 1, r)
                else:
                    a, b = loc[old][l1 - 1]
                    c, d = loc[old][l1]
                    if pos[0] == b:
                        del_seg(old, a, b)
                        add_seg(old, a, b-1)
                    elif pos[0] == c:
                        del_seg(old, c, d)
                        add_seg(old, c+1, d)
                    elif pos[0] < b:
                        del_seg(old, a, b)
                        add_seg(old, a, pos[0]-1)
                        add_seg(old, pos[0]+1, b)
                    elif pos[0] > c:
                        del_seg(old, c, d)
                        add_seg(old, c, pos[0]-1)
                        add_seg(old, pos[0]+1, d)

            if not loc[new]:
                add_seg(new, *pos)
                ans[k] = max_len[-1]
                continue
            l2 = bisect.bisect_left(loc[new], pos)
            if l2 == 0:
                l, r = loc[new][l2]
                if l - pos[0] > 1:
                    add_seg(new, *pos)
                else:
                    del_seg(new, l, r)
                    add_seg(new, pos[0], r)

            elif l2 == len(loc[new]):
                l, r = loc[new][l2 - 1]
                if pos[0] - r > 1:
                    add_seg(new, *pos)
                else:
                    del_seg(new, l, r)
                    add_seg(new, l, pos[0])
            else:
                a, b = loc[new][l2 - 1]
                c, d = loc[new][l2]
                if pos[0] - b == 1 and c - pos[0] == 1:
                    del_seg(new, a, b)
                    del_seg(new, c, d)
                    add_seg(new, a, d)
                elif pos[0] - b == 1:
                    del_seg(new, a, b)
                    add_seg(new, a, pos[0])
                elif c - pos[0] == 1:
                    del_seg(new, c, d)
                    add_seg(new, pos[0], d)
                else:
                    add_seg(new, *pos)
            ans[k] = max_len[-1]
        return ans
3586