package leetcode;

/**
 * https://leetcode.com/problems/largest-substring-between-two-equal-characters/
 */
public class Problem1624 {
    public int maxLengthBetweenEqualCharacters(String s) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1624 prob = new Problem1624();
        System.out.println(prob.maxLengthBetweenEqualCharacters("aa")); // 0
        System.out.println(prob.maxLengthBetweenEqualCharacters("abca")); // 2
        System.out.println(prob.maxLengthBetweenEqualCharacters("cbzxy")); // -1
        System.out.println(prob.maxLengthBetweenEqualCharacters("cabbac")); // 4
        System.out.println(prob.maxLengthBetweenEqualCharacters("aaaaa")); // 3
        System.out.println(prob.maxLengthBetweenEqualCharacters("abdefghdxyz")); // 4
        System.out.println(prob.maxLengthBetweenEqualCharacters("aaxyza")); // 4
    }
}

package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/largest-substring-between-two-equal-characters/
 */
public class Problem1624 {
    public int maxLengthBetweenEqualCharacters(String s) {
        int[] indexes = new int[26];
        Arrays.fill(indexes, -1);
        for (int i = s.length() - 1; i >= 0; i--) {
            int index = indexes[s.charAt(i) - 'a'];
            if (index == -1) {
                indexes[s.charAt(i) - 'a'] = i;
            }
        }
        int answer = -1;
        for (int i = 0; i < s.length(); i++) {
            int index = indexes[s.charAt(i) - 'a'];
            if (index > i) {
                answer = Math.max(answer, index - i - 1);
            }
        }
        return answer;
    }
}

Solve problem 1624