"""
main idea: use dictionar to get char that appeared more than once. store its index in order, get max index difference so far
"""
class Solution:
    def maxLengthBetweenEqualCharacters(self, s: str) -> int:
        d = collections.defaultdict(list)
        res = 0
        flag = False
        for idx,ch in enumerate(s):
            d[ch].append(idx)
        for ch, li in d.items():
            if len(li)> 1:
                flag = True
                res = max(res,li[-1] - li[0]-1)
        return -1 if not flag else res