1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int maxLengthBetweenEqualCharacters(String s) { int[] indexMap = new int[26]; Arrays.fill(indexMap, -1);
int maxLength = -1; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int prevIndex = indexMap[c - 'a']; if (prevIndex != -1) { maxLength = Math.max(maxLength, i - prevIndex - 1); } else { indexMap[c - 'a'] = i; } }
return maxLength; } }
|
该题题目描述不清,未说明两个相同字符之间的最长子字符串可以包含两侧字符。
References
1624. Largest Substring Between Two Equal Characters