- public int maxLengthBetweenEqualCharacters(String s) {
- int n=s.length();
- int[] l=new int[26];
- int[] r=new int[26];
- Arrays.fill(l,-1);
- Arrays.fill(r,-1);
- for(int i=0;i<n;i++){
- char c1=s.charAt(i);
- char c2=s.charAt(n-1-i);
- if(l[c1-'a']==-1)
- l[c1-'a']=i;
- if(r[c2-'a']==-1)
- r[c2-'a']=n-1-i;
- }
- int ans=-1;
- for(int i=0;i<26;i++){
- ans=Math.max(ans,r[i]-l[i]-1);
- }
- return ans;
- }