Largest Substring Between Two Equal Characters
给一个字符串s, 求一种相同的字符之间的字符的最长长度.
先找到一个个字符的位置, 然后计算长度.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Solution { public int maxLengthBetweenEqualCharacters(String s) { int[] ary = new int[26]; Arrays.fill(ary, -1); int res = -1; for(int i = 0; i < s.length(); i++) { if(ary[s.charAt(i) - 'a'] == -1) { ary[s.charAt(i) - 'a'] = i; } else { res = Math.max(res, i - ary[s.charAt(i) - 'a'] - 1); } } return res; } } |